Forums Forums Search & Filter Pro Search not working with ACF fields in wp_query

Tagged: , , ,

Viewing 10 posts - 1 through 10 (of 11 total)
  • Anonymous
    #200152

    I want to get search results based on the pages ACF fields which set the post_type and search_filter_id

    It works on page load but then when changing the search criteria, it seems to just load all uncategorized posts and forgets the fields for post type and id. Here is my code:

    <?php echo do_shortcode('[searchandfilter id="'.get_field('search_id').'"]'); ?>
    <div class="buy-wrapper" id="search-results">
    
    <?php
    //$postType = get_field('post_type');
    //echo $postType;
    $args = array('post_type' => get_field('post_type'));
    $args['search_filter_id'] = get_field('search_id');
    $category_posts = new WP_Query($args);

    It’s worth noting this works fine, when hard coded post type and IDs are used. Is there a work around for this?

    Trevor
    #200155

    Is it just these two lines that do not work?

    $args = array('post_type' => get_field('post_type'));
    $args['search_filter_id'] = get_field('search_id');

    If you use an echo with a message to prefix those lines, do the post type and id output or are they empty?

    Does the form in the first line of the code output OK?

    What happens if you add the Post ID as the second argument. E.g.

    $args = array('post_type' => get_field('post_type', get_the_ID()));
    $args['search_filter_id'] = get_field('search_id', get_the_ID());
    Anonymous
    #200161

    I have made the code a little cleaner, I tried get_the_ID() and this did not work, I also have taken the variables out of the AJAX container (#search-results) and still get the same issue.

    The form in the first line does work fine, it is literally just the results that go to all uncategorised. Echoing those 2 args, produces the correct array and ID.

    
          <?php
          $postType = get_field('post_type', get_the_ID());
          $searchId = get_field('search_id', get_the_ID());
    
          echo do_shortcode('[searchandfilter id="'.$searchId.'"]'); 
    
          $args = array('post_type' => $postType);
          $args['search_filter_id'] = $searchId;
    
          print_r($args = array('post_type' => $postType));
          echo $args['search_filter_id'] = $searchId;
          ?>
    
          <div class="buy-wrapper" id="search-results">
          <?php
          $category_posts = new WP_Query($args);
    
          if($category_posts->have_posts()) : 
            while($category_posts->have_posts()) : 
               $category_posts->the_post();
          ?>
    Trevor
    #200165

    If, instead of these lines:

    print_r($args = array('post_type' => $postType));
    echo $args['search_filter_id'] = $searchId;

    You do this:

    echo "<pre>";
    print_r($args);
    echo "</pre>";

    Does the second search look right?

    Anonymous
    #200172

    The data is blank after the search, you are right. Thank-you for your help so far, what do you suggest?

    Array
    (
    [post_type] =>
    [search_filter_id] =>
    )

    ;

    
          <?php
          $postType = get_field('post_type', get_the_ID());
          $searchId = get_field('search_id', get_the_ID());
    
          echo do_shortcode('[searchandfilter id="'.$searchId.'"]'); 
          ?>
    
          <div class="buy-wrapper" id="search-results">
          <?php
          $args = array('post_type' => $postType);
          $args['search_filter_id'] = $searchId;
          ?>
          <pre><?php print_r($args);?></pre>;
    
          <?php
          
          $category_posts = new WP_Query($args);
    
          if($category_posts->have_posts()) : 
            while($category_posts->have_posts()) : 
               $category_posts->the_post();
          ?>
    Trevor
    #200218

    Sorry, my code to you was broken, but you got there anyway 🙂

    Are you able to send me a live link/URL to your search page so I can take a look?

    Anonymous
    #200226
    This reply has been marked as private.
    Trevor
    #200236
    This reply has been marked as private.
    Anonymous
    #200242
    This reply has been marked as private.
    Trevor
    #200244

    From your original note, I got that you would be using more than one real page, yes? The problem is that the ‘As an archive’ method is designed NOT to be used with existing pages, but pages that WordPress creates ‘on the fly’ and which do not have Post IDs and therefore cannot have custom fields.

    The only Display method suitable for use as you intend is the ‘Custom’ method, but that requires one form per page (as the results URL is fixed). I have used this method many times in the past, with an ACF field on the page to store the form ID and other stuff. Then the do_shortcode calls that variable to complete the shortcode).

Viewing 10 posts - 1 through 10 (of 11 total)