Created
March 3, 2022 17:49
-
-
Save markhowellsmead/54b78b4070a18562e656f326a27fa214 to your computer and use it in GitHub Desktop.
Extend WordPress admin media search to include custom meta fields
This file contains hidden or 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 | |
add_action( 'pre_get_posts', 'extend_admin_search' ); | |
function extend_admin_search( $query ) { | |
$post_type = 'post'; | |
$custom_fields = array("source",); | |
if( ! is_admin() ) | |
return; | |
if ( $query->query['post_type'] != $post_type ) | |
return; | |
$search_term = $query->query_vars['s']; | |
$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 ); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment