I’ve a customized submit sort that makes use of a number of meta fields. Within the admin space I would really like to have the ability to search by these meta fields. I’ve applied this at present in my features.php with this code:
perform custom_search_query( $question ) {
if ( is_admin() && is_main_query() && $query->is_search ) {
$query->set('meta_query', array(
"relation" => "OR",
array(
'key' => 'first_name',
'worth' => $query->query_vars['s'],
'examine' => 'LIKE'
),
array(
'key' => 'last_name',
'worth' => $query->query_vars['s'],
'examine' => 'LIKE'
),
array(
'key' => 'e-mail',
'worth' => $query->query_vars['s'],
'examine' => 'LIKE'
)
));
$query->set('post_type', 'utility'); // elective
};
}
add_filter( 'pre_get_posts', 'custom_search_query');
The issue is that with these code if I seek for say the identify “walker” and I’ve information of my customized submit sort that match that standards within the meta area I get again no outcomes. The rationale this seems to be occurring is that the SQL that’s being generated by the question that will get executed is that this:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id) WHERE 1=1
AND (((wp_posts.post_title LIKE '%walker%') OR (wp_posts.post_content LIKE '%walker%')))
AND wp_posts.post_type = 'utility' AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft'
OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'non-public')
AND ((wp_postmeta.meta_key = 'first_name' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%walker%')
OR (mt1.meta_key = 'last_name' AND CAST(mt1.meta_value AS CHAR) LIKE '%walker%')
OR (mt2.meta_key = 'e-mail' AND CAST(mt2.meta_value AS CHAR) LIKE '%walker%') )
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 20
The question is trying to go looking each the submit title and content material AND my customized fields. I do not need it to be on the lookout for matches within the title or content material. How can I get the question to cease doing that and thereby returning the restuls that aI need?