Skip to content

Instantly share code, notes, and snippets.

@birgire
Last active July 6, 2021 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save birgire/4a43dbe1b7566aa0f162b59066276d15 to your computer and use it in GitHub Desktop.
Save birgire/4a43dbe1b7566aa0f162b59066276d15 to your computer and use it in GitHub Desktop.
WordPress Plugin:Search for name, slug or description in the tag search in /wp-admin/edit-tags.php?taxonomy=post_tag.
<?php
/**
* Plugin Name: Extend the tag search
* Description: Search for name, slug or description in the tag search in /wp-admin/edit-tags.php?taxonomy=post_tag.
* Plugin URI: https://gist.github.com/birgire/4a43dbe1b7566aa0f162b59066276d15
* Author: birgire
* Version: 1.0.0
*/
/**
* Target the correct get_terms() instance on edit-tags.php and replace the search with the
* custom _name_or_slug_or_description__like attribute
*/
add_filter( 'get_terms_args', function( $args, $taxonomies )
{
if(
did_action( 'load-edit-tags.php' ) // Target the edit-tags.php
&& 'post_tag' === filter_input( INPUT_GET, 'taxonomy' ) // Target post tags
&& isset( $args['search'] ) // Target the search mode
&& 'all' === $args['fields'] // Exclude the get_terms() instance with term count
&& ! isset( $args['largest'] ) // Exclude the get_terms() instance within wp_tag_cloud()
) {
$args['_name_or_slug_or_description__like'] = $args['search'];
unset( $args['search'] );
}
return $args;
}, 10, 2 );
/**
* Support the custom _name_or_slug_or_description__like attribute of get_terms()
*/
add_filter( 'terms_clauses', function( $clauses, $taxonomies, $args ) use ( &$wpdb )
{
if(
! empty( $args['_name_or_slug_or_description__like'] )
&& ! empty( $clauses['where'] )
) {
$like = '%' . $wpdb->esc_like( $args['_name_or_slug_or_description__like'] ) . '%';
$clauses['where'] .= $wpdb->prepare(
" AND ( t.name LIKE %s OR t.slug LIKE %s OR tt.description LIKE %s ) ",
$like,
$like,
$like
);
}
return $clauses;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment