I have added custom taxonomy field on tags, but when I search that field search is not working. Any way to make it searchable in WordPress dashboard?
-
Wait. Is it field for custom taxonomy terms, or custom post type posts? Do you want to include it in search in admin, or on frontend? – Krzysiek Dróżdż♦ Jan 10 '19 at 8:34
-
If you are confused simply just install this plugin wordpress.org/plugins/codepress-admin-columns and show the result. – Tejas Gajjar Jan 10 '19 at 9:43
-
This will give the option to search a collumn as per your request – Tejas Gajjar Jan 10 '19 at 9:43
-
This plugin for search posts but I want to search tags column. – Krunal Jan 10 '19 at 9:44
Here’s the code. You can change $post_type and $custom_fields according to your needs.
function extend_admin_search( $query ) {
// Extend search for document post type
$post_type = 'document';
// Custom fields to search for
$custom_fields = array(
"_file_name",
);
if( ! is_admin() )
return;
if ( $query->query['post_type'] != $post_type )
return;
$search_term = $query->query_vars['s'];
// Set to empty, otherwise it won't find anything
$query->query_vars['s'] = '';
if ( $search_term != '' ) {
$meta_query = array( 'relation' => 'OR' );
foreach( $custom_fields as $custom_field ) {
array_push( $meta_query, array(
'key' => $custom_field,
'value' => $search_term,
'compare' => 'LIKE'
));
}
$query->set( 'meta_query', $meta_query );
};
}
add_action( 'pre_get_posts', 'extend_admin_search' );
-
I have tried but not working. $query->query_vars['s'] is empty and got different post type from the url. url is ".../edit-tags.php?taxonomy=post_tag&post_type=post&s=wed" – Krunal Jan 10 '19 at 7:51
-
Kindly let me know your post type as well as taxonomy name so I can help you into it. – Pratik bhatt Jan 10 '19 at 9:06