Created
April 27, 2022 05:30
-
-
Save encoderit-arman/5f808f347490088d76ed2993b18030da to your computer and use it in GitHub Desktop.
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
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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment