Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Plugin: Search For Name Or Description In The Quick Search For Tags In The Menu Editor
<?php
/**
* Plugin Name: Quick Search Menu Enhancement
* Description: Search for name or description in the quick-search for tags in the menu editor
* Plugin URI: https://gist.github.com/birgire/b7001f51b3ad970f37fee2b8f854d939
* Author: birgire
* Version: 1.0.0
*/
/**
* Replace the name__like with the custom _name_or_description__like attribute of get_terms()
* during the post_tag search in the menu backend page.
*/
add_filter( 'get_terms_args', function( array $args, array $taxonomies )
{
if(
doing_action( 'wp_ajax_menu-quick-search' )
&& 'quick-search-taxonomy-post_tag' === filter_input( INPUT_POST, 'type' )
&& isset( $args['name__like'] )
) {
$args['_name_or_description__like'] = $args['name__like'];
unset( $args['name__like'] );
}
return $args;
}, 10, 2 );
/**
* Support the custom _name_or_description__like attribute of get_terms()
*/
add_filter( 'terms_clauses', function( $clauses, $taxonomies, $args ) use ( &$wpdb )
{
if(
! empty( $args['_name_or_description__like'] )
&& ! empty( $clauses['where'] )
) {
$like = '%' . $wpdb->esc_like( $args['_name_or_description__like'] ) . '%';
$clauses['where'] .= $wpdb->prepare(
" AND ( t.name LIKE %s OR tt.description LIKE %s ) ",
$like,
$like
);
}
return $clauses;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment