Looking at including the content from metaboxes in my search results but after researching I was only able to pick up two solutions:
How can I include meta box content when searching? Suggests using WP_Query
but you have to code for every key:
$aquarium_H = array(
'key' => 'aquarium_H', //name of your meta field
'value' => $_GET["s"], // value from WordPress search bar. this is sanitized by WP
'type' => 'numeric', // string/numeric/whatever
'compare' => '<=' // this can be "LIKE" or "NOT LIKE". most mySQL operators
);
$meta_query[] = $aquarium_H; // add to meta_query array
and question How to make search include data from wp_postmeta? suggests a similar approach with:
function me_search_query( $query ) {
if ( $query->is_search ) {
$meta_query_args = array(
array(
'key' => 'your_key',
'value' => $query->query_vars['s'] = '',
'compare' => 'LIKE',
),
);
$query->set('meta_query', $meta_query_args);
};
}
add_filter( 'pre_get_posts', 'me_search_query');
The issue is if you have a large number of keys you'll have bloated code. Is there not a cleaner way now to call each key or to include everything in 4.8?
Looking at including the content from metaboxes in my search results but after researching I was only able to pick up two solutions:
How can I include meta box content when searching? Suggests using WP_Query
but you have to code for every key:
$aquarium_H = array(
'key' => 'aquarium_H', //name of your meta field
'value' => $_GET["s"], // value from WordPress search bar. this is sanitized by WP
'type' => 'numeric', // string/numeric/whatever
'compare' => '<=' // this can be "LIKE" or "NOT LIKE". most mySQL operators
);
$meta_query[] = $aquarium_H; // add to meta_query array
and question How to make search include data from wp_postmeta? suggests a similar approach with:
function me_search_query( $query ) {
if ( $query->is_search ) {
$meta_query_args = array(
array(
'key' => 'your_key',
'value' => $query->query_vars['s'] = '',
'compare' => 'LIKE',
),
);
$query->set('meta_query', $meta_query_args);
};
}
add_filter( 'pre_get_posts', 'me_search_query');
The issue is if you have a large number of keys you'll have bloated code. Is there not a cleaner way now to call each key or to include everything in 4.8?
Share Improve this question edited Sep 20, 2017 at 15:01 user9447 asked Aug 12, 2017 at 4:45 user9447user9447 1,7927 gold badges30 silver badges55 bronze badges 2- If you have a large number of keys, then the bloated-ness of the code will reflect the performance you'll get from the search. WordPress' database isn't well optimised for complex searches. If you need to search a large amount of meta, I suggest a plugin like SearchWP which builds a separate index that makes searching meta much more efficient. – Jacob Peattie Commented Aug 12, 2017 at 12:35
- instead of using a plugin Id rather research and learn if I can generate a JSON file and then search within that file from my posts. – user9447 Commented Aug 12, 2017 at 15:34
1 Answer
Reset to default -2It's a duplicate of How to make search include data from wp_postmeta? (somewhy couldn't mark it as a duplicate).