Created
June 14, 2016 21:57
-
-
Save emzo/6f86f50199c09d2f4ce6863401a307fb to your computer and use it in GitHub Desktop.
Allow linking to taxonomy terms as well as posts when performing internal linking
This file contains 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 | |
add_filter( 'wp_link_query', 'seren_wp_link_query_term_linking', 99, 2 ); | |
function seren_wp_link_query_term_linking( $results, $query ) { | |
// Query taxonomy terms. | |
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'names' ); | |
$terms = get_terms( $taxonomies, array( | |
'name__like' => $query['s'], | |
'number' => 20, | |
'hide_empty' => false, | |
) ); | |
// Check if any taxonomies were found. | |
if ( ! empty( $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, | |
); | |
} | |
} | |
return $results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @emzo
I've came across this when searching how to add taxonomies to the wp Insert/edit link popup layer. Did you ever experience an infinite loop of duplicated returned terms?