0

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?

  • 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 Aug 12 '17 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. – DᴀʀᴛʜVᴀᴅᴇʀ Aug 12 '17 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.