Created
January 5, 2023 14:11
-
-
Save oliver-la/89ec4c473ef37cef22764a5d4e060230 to your computer and use it in GitHub Desktop.
WordPress: Include Taxonomies in Editor Link Selection Modal at the backend
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 | |
/** | |
* Filters the link query results. (Link Edit Modal in the editor) | |
* @see https://github.com/WordPress/WordPress/blob/3ba44120d0ffa7ac330a1b7e3f8b363ce78f8060/wp-includes/class-wp-editor.php#L1767 | |
*/ | |
add_filter( | |
'wp_link_query', function( array $results, array $query ) : array { | |
$terms = new WP_Term_Query( | |
[ | |
'taxonomy' => ['product_category', 'room', 'brand', 'category'], | |
'search' => $query['s'], | |
'relevanssi' => true, // Only if using relevanssi (improves search accuracy drastically) | |
'hide_empty' => false, | |
'update_term_meta_cache' => false, | |
'number' => 20, // Note: You cannot change this, as the original post query hardcodes 20 | |
'offset' => $query['offset'] | |
] | |
); | |
/** | |
* @var WP_Term | |
*/ | |
foreach ($terms->terms as $term) { | |
$results[] = [ | |
'ID' => 'term_' . $term->term_id, | |
'title' => trim(esc_html(strip_tags($term->name))), | |
'permalink' => get_term_link($term), | |
'info' => $term->taxonomy | |
]; | |
} | |
usort( | |
$results, function($a, $b) { | |
return strnatcasecmp($a['title'], $b['title']); | |
} | |
); | |
return $results; | |
}, 10, 2 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment