Home › Forums › Backend Issues (wp-admin) › Field / SubField Searching within Admin Panel › Reply To: Field / SubField Searching within Admin Panel
This certainly can work but I’d like to keep the content area as-is for excerpt functionality. I did find a solution but it looks like it searches all post_meta, so it’s not something that can specifically be set.
I would like to see a “Searchable” Boolean added to field types.
// Search on custom fields
function custom_search_join ($join){
global $pagenow, $wpdb;
$types = ['course'];
// I want the filter only when performing a search on edit page of Custom Post Type in $types array
if ( is_admin() && $pagenow=='edit.php' && in_array( $_GET['post_type'], $types ) && isset( $_GET['s'] ) ) {
$join .='LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}
return $join;
}
add_filter('posts_join', 'custom_search_join' );
function custom_search_where( $where ){
global $pagenow, $wpdb;
$types = ['course'];
// I want the filter only when performing a search on edit page of Custom Post Type in $types array
if ( is_admin() && $pagenow=='edit.php' && in_array( $_GET['post_type'], $types ) && isset( $_GET['s'] ) ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
}
return $where;
}
add_filter( 'posts_where', 'custom_search_where' );
function custom_search_distinct( $where ){
global $pagenow, $wpdb;
$types = ['course'];
if ( is_admin() && $pagenow=='edit.php' && in_array( $_GET['post_type'], $types ) && isset( $_GET['s'] ) ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'custom_search_distinct' );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.