0

I have a recipe site where each recipe resides in a custom meta field called "recipes". I only want the contents of the meta field searched when users search the site.

The problem with my code below: When the number of search results exceeds the number of posts per page (10 - from main WP settings), it returns the results 3 times. In other words, if the search has 11 results the sequence repeats 3 times. No active plugins.

function my_search_filter( $query ) {
    global $searchtest;
    global $searchcount;
    if ($query->is_search) {
        $args = array(
                'post_type' => 'post',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'ignore_sticky_posts' => TRUE,
                'meta_key' => 'recipe',
                'meta_value' => sanitize_text_field($query->query_vars['search']), 
                'meta_compare' => 'LIKE'
        );
        $searchtest = new WP_Query ($args);
        if ($searchtest->have_posts()) { while ($searchtest->have_posts()) { $searchtest->the_post();  
            $searchtestresults[] = get_the_ID();
        }} wp_reset_postdata();
        $searchcount = count($searchtestresults);
    }
}
add_filter( 'pre_get_posts', 'my_search_filter' );

And in search.php

if ($searchtest->have_posts()) { while ($searchtest->have_posts()) {$searchtest->the_post();
...
post content
...
<div class="post-nav">
    <div id="post-nav-next"><?php next_posts_link('Older posts') ?></div>
    <div id="post-nav-prev"><?php previous_posts_link('Newer posts') ?></div>
</div><!-- /post-nav -->                

This is still in development my local WAMP, so I have no link to provide. I have looked at related posts here and elsewhere on searching custom meta fields, and this code above is the closest I have come to getting the required results. I appreciate any help or alternate solutions.

0

You are abusing pre_get_posts badly with that code. With pre_get_posts the point is to alter the main query. By doing that you reduce the number of necessary queries and save some processing time, plus keep the WordPress globals-- $wp_query, for example-- neat for any other code that might need them. What you are doing is creating new globals and populating them with new queries. You are just hiding those new queries inside pre_get_posts.

If I understand what you are trying to accomplish, I believe you need this:

function search_meta_only_wpse_198762($qry) {
  if ($qry->is_main_query() && $qry->is_search()) {
    $qry->set(
      'meta_query', 
      array(
        array(
          'key' => 'recipe',
          'value' => sanitize_text_field($query->query_vars['search']),
          'compare' => 'LIKE'
        )
      )
    );
    $qry->set('s',null);
  }
}
add_filter('pre_get_posts','search_meta_only_wpse_198762');

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.