Just wondering if there’s anything in sage 9 that might affect meta_query in search queries?
I’m trying to include some custom fields in search for front & back ends. Something I’ve done many times before.
However it doesn’t search the custom fields and stops searching the main fields. I’ve created and exact query with get_posts() which is in App.php which works fine. Can’t help but think its something to do with pre_get_posts or the location of the functions.
in search.php added like so -
array_map(function ($file) use ($sage_error) {
$file = "../app/{$file}.php";
if (!locate_template($file, true, true)) {
$sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file), 'File not found');
}
}, ['helpers', 'setup', 'file-managment','filters', 'admin','custom-fields','search','debugging']);
add_filter('pre_get_posts', __NAMESPACE__ . '\\search_custom_fields');
// add_action('pre_get_posts', __NAMESPACE__ . '\\search_custom_fields');
function search_custom_fields($query)
{
if ($query->is_search && $query->is_main_query()) {
echo "search_custom_fields: " . $query->is_main_query() . " " . $query->is_search . "\n";
echo "query->post_type: " . get_query_var('post_type') . "\n";
echo "Setting meta_query in search_custom_fields. " . "\n\n\n";
// i want the search to include custom fields as well as continue to include the title and post content. So i want to return ANY matches from any of the fields
$search_word = get_query_var('s'); // 'test' for post title and content.
$query->set('post_type', 'location'); // search is only for post type 'locations'
$query->set('orderby', 'date');
$query->set('order', 'DSC');
// this taxonomy test works!
$tax = array(
array(
'taxonomy' => 'location_facilities',
'field' => 'slug',
'terms' => 'inside-m25',
));
$query->set('tax_query', $tax);
// acf field is called 'loc_refid'
// this stops all results
$meta_query1 = [
[
'key' => 'loc_refid',
'value' => $search_word,
'compare' => 'LIKE',
],
];
// returns general matches but nothing for the loc_refid field
$meta_query2 = [
'key' => 'loc_refid',
'value' => $search_word,
'compare' => 'LIKE',
];
// // this stops all results
$meta_query3 = [
'relation' => 'OR',
[
'key' => 'loc_refid',
'value' => $search_word,
'compare' => 'LIKE',
],
];
// i have several fields to add once over this hurdle.
$meta_query4 = array(
'relation' => 'OR',
array(
'key' => 'loc_refid',
'value' => $search_word,
'compare' => 'LIKE',
),
array(
'key' => 'location_postcode_preview',
'value' => $search_word,
'compare' => 'LIKE',
),
);
$meta_query_new = array('relation' => 'OR');
$meta_query_new[] = array(
'key' => 'loc_refid',
'value' => $search_word,
'compare' => 'LIKE',
);
$query->set('meta_query', $meta_query_new);
print_r($query);
}
return $query;
// if using add_action no need to return
}
print_r($query);
WP_Query Object
(
[query] => Array
(
[s] => test
)
[query_vars] => Array
(
[s] => test
...
[post_type] => location
[orderby] => date
[order] => DSC
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => location_facilities
[field] => slug
[terms] => inside-m25
)
)
[meta_query] => Array
(
[relation] => OR
[0] => Array
(
[key] => loc_refid
[value] => test
[compare] => LIKE
)
)
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
)
[relation] => AND
[table_aliases:protected] => Array
(
)
[queried_terms] => Array
(
)
[primary_table] =>
[primary_id_column] =>
)
[meta_query] =>
[date_query] =>
[post_count] => 0
[current_post] => -1
...
thanks, D.