Cannot filter on custom wp post

Forums › Forums › Search & Filter Pro › Cannot filter on custom wp post

Viewing 10 posts – 1 through 10 (of 15 total)
  • Anonymous
    #198330

    Hello,
    I recently bought this plugin but I am having problems starting it work.

    I have custom posts called ‘trainer’ with custom fields (ACF).
    I want to display filtering on custom template (archive-trainer.php), where right now I have:

     <?php 
                echo do_shortcode('[searchandfilter id="1513"]');
                echo get_trainers(); 
    ?>

    The function get_trainers contains custom WP query

    function get_trainers()
    {
        $html = '';
        
        $args = array(
        'showposts' => 10,
        'post_type' => 'trainer',
        );
    
        // attach S&F
        do_action("search_filter_query_posts", 1513);
    
        $query = new WP_Query( $args );
        $trainers = $query->posts;
        var_dump( $GLOBALS['wp_query']->request);
    var_dump($trainers);
    
    }

    In S&F admin I have set display results method: custom and I have provided correct url of the page where the archive is.

    If I var_dump last query, no S&F filtering is performed ๐Ÿ™
    Could you please give me a hint what I am missing? ๐Ÿ™‚

    Thanks
    Karel

    Trevor
    #198339

    If you remove the line:

    do_action("search_filter_query_posts", 1513);

    do you get results?

    Anonymous
    #198347

    I do get full set of results, when this line is or is not present.
    The only difference is when the line is present is this statement in SQL wp_posts.ID IN (1284,1495,1503,1504,1505,1506,1508) (and this statement is there if i try filtering anything…

    Trevor
    #198353

    Ah, I see. I misunderstood your initial question. I am sorry for that. At least, I think I understand.

    Showing the word searched for is not so difficult. If you have other filters, then it becomes a little more complex, but I can give you links. The documentation page:

    https://searchandfilter.com/documentation/accessing-search-data/

    This thread might help you:

    https://support.searchandfilter.com/forums/topic/accessing-field-slug-on-search-results/

    โ€ฆ and this search will give similar threads I think:

    https://support.searchandfilter.com/forums/search/sf_current_query+get_array+field+%5Bvalue%5D/

    Anonymous
    #198357

    Well I might have asked wrong way ๐Ÿ™‚

    The thing is the filter as I call it with the shortcode shows up, but if I try to filter results (e.g. I select one region in the dropdown or white a text to the search input and press submit), nothing changes – the result set of $trainers remains unchanged (unfiltered), so as the $GLOBALS[‘wp_query’]->request).

    From your first link you provided, I tried this code:

     $sf_current_query = $searchandfilter->get(1513)->current_query();
        echo '<pre>';
        print_r($sf_current_query->get_array());
        echo '</pre>';

    and got

    array (size=10)
      'paged' => int 1
      'search_filter_id' => int 1513
      'search_filter_override' => boolean false
      'posts_per_page' => int 10
      'post_status' => 
        array (size=1)
          0 => string 'publish' (length=7)
      'meta_query' => 
        array (size=0)
          empty
      'post_type' => string 'trainer' (length=7)
      'is_search' => boolean true
      'is_archive' => boolean true
      'post__in' => 
        array (size=7)
          0 => int 1284
          1 => int 1495
          2 => int 1503
          3 => int 1504
          4 => int 1505
          5 => int 1506
          6 => int 1508

    (notice there is no sign of filtering in the array). This is my main problem.

    Anonymous
    #198359

    It seems the filtering request (for example I write ‘Jane’ to the search input, or select region 1) does not come to the query. I believe I went through the documentation but I am not sure if I am missing something how to link the S&F filter with the WP Query so the filtering would work…

    Trevor
    #198362

    Ah, OK

    Instead of using do_action, you can add the filter to the args. Here is an example:

    $args = array(
        'showposts' => 10,
        'post_type' => 'trainer',
        'search_filter_id' => '1513',
    );

    I assume your form ID is 1513.

    Anonymous
    #198376

    Thanks Trevor. Yes, the form ID is 1513.

    I made a mistake. With or without ‘search_filter_id’ => ‘1513’ or do_action, I receive an empty array.

    (The dump I sent before was another debug from the core of your plugin, sorry for mistake.)

    Trevor
    #198378

    Can you do a test using the shortcode method instead (no coding required)? And see if the filter works correctly?

    Anonymous
    #198390

    I created a new page called Test, inserted

    [searchandfilter id="1513"]
    [searchandfilter id="1513" show="results"]

    then in S&F admin changed Display results method using shortcode and changed result url

    And the filter does not work the very same way as on custom template with custom WP query ๐Ÿ™
    I even tried to deactivate all plugins ๐Ÿ™

Viewing 10 posts – 1 through 10 (of 15 total)

Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.