Created
January 29, 2018 18:08
-
-
Save carasmo/133d33a98ee66171df9132ec0f2168c7 to your computer and use it in GitHub Desktop.
Add Custom Post Type and Taxonomy Terms to wp_link_query. Link search function in WordPress editor add Custom Post Types and Taxonomy Terms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//don't re add the php tag above | |
add_filter('wp_link_query', 'cab_add_custom_post_type_archive_link', 10, 2); | |
/** | |
* Add Custom Post Type archive to WordPress search link query | |
* Author: https://github.com/mthchz/editor-archive-post-link/blob/master/editor-archive-post-link.php | |
*/ | |
function cab_add_custom_post_type_archive_link( $results, $query ) { | |
if ( $query['offset'] > 0 ) : // Add only on the first result page | |
return $results; | |
endif; | |
$match = '/' . str_remove_accents( $query['s'] ) . '/i'; | |
foreach( $query['post_type'] as $post_type ) : | |
$pt_archive_link = get_post_type_archive_link($post_type); | |
$pt_obj = get_post_type_object($post_type); | |
if ( $pt_archive_link !== false && $pt_obj->has_archive !== false ) : // Add only post type with 'has_archive' | |
if ( preg_match( $match, str_remove_accents( $pt_obj->labels->name ) ) > 0 ) : | |
array_unshift( $results, array( | |
'ID' => $pt_obj->has_archive, | |
'title' => trim( esc_html( strip_tags($pt_obj->labels->name) ) ) , | |
'permalink' => $pt_archive_link, | |
'info' => 'Archive', | |
) ); | |
endif; | |
endif; //end post type archive links in link_query | |
endforeach; | |
return $results; | |
} | |
//* Remove accents | |
function str_remove_accents( $str, $charset='utf-8' ) { | |
$str = htmlentities($str, ENT_NOQUOTES, $charset); | |
$str = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str); | |
$str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); | |
$str = preg_replace('#&[^;]+;#', '', $str); | |
return $str; | |
} | |
add_filter( 'wp_link_query', 'cab_wp_link_query_term_linking', 99, 2 ); | |
/** | |
* Add Term links to WordPress search link query | |
* Modified from: https://gist.github.com/emzo/6f86f50199c09d2f4ce6863401a307fb | |
* Ref: https://codex.wordpress.org/Function_Reference/get_taxonomies | |
* https://developer.wordpress.org/reference/classes/wp_term_query/__construct/ | |
* https://developer.wordpress.org/reference/functions/get_terms/ | |
* http://php.net/manual/en/function.array-diff.php | |
*/ | |
function cab_wp_link_query_term_linking( $results, $query ) { | |
//* Query taxonomy terms. | |
$taxonomies = get_taxonomies( array( | |
'show_in_nav_menus' => true | |
) , 'names'); | |
//* Add to the array any taxonomies you do not want | |
$exclude = array( | |
'media_category', | |
'tag', | |
); | |
$taxonomies = array_diff( $taxonomies, $exclude ); | |
//* Get the terms of the taxonomies | |
$terms = get_terms( $taxonomies, array( | |
'name__like' => $query['s'], | |
'number' => 20, | |
'hide_empty' => true, | |
)); | |
//* Terms | |
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ): | |
foreach( $terms as $term ): | |
$results[] = array( | |
'ID' => 'term-' . $term->term_id, | |
'title' => html_entity_decode($term->name, ENT_QUOTES, get_bloginfo('charset')) , | |
'permalink' => get_term_link(intval($term->term_id) , $term->taxonomy) , | |
'info' => get_taxonomy($term->taxonomy)->labels->singular_name, | |
); | |
endforeach; | |
endif; | |
return $results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment