Forums Forums Search & Filter Pro Count of filters not correct after using pre_get_posts

Tagged: 

Viewing 8 posts - 1 through 8 (of 8 total)
  • Anonymous
    #240180

    Hello,

    We are using ‘pre_get_posts’ to limit the posts that we want to show on the archive of a post type.

    We are also using a S&F form on this archive, with several filters (taxonomies and post meta).

    I’ve activated the auto count option. But this count shows the wrong values. It does not take the ‘pre_get_posts’ changes into consideration.

    Is there a way to fix this?

    Thanks in advance!
    -Kevin

    Trevor
    #240256

    Ideally, instead of pre_get_posts (which fires after we do our stuff, so, as you see, the counts will be wrong), can you see if you can apply those limitations using this filter:

    https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments

    Anonymous
    #242064

    Hi Trevor,

    That’s working for me, thanks!

    I does seem that the hooked in function is running twice. That’s not ideal, because some query changes are also added twice this way. How can we fix this?

    Ross Moderator
    #242066

    Hi Kevin

    For autocount to work this needs to be done twice.

    We do our search query (in the background outside of WP_Query) once, then we feed the results into the the post__in agument of two WP_Query, one for auto count (which needs to know about the total number of results in order to get accurate count numbers, and this query only fetches post IDs, not the whole post object, so its much more efficient) and once for the regular query for your actual results, which is limited in size due to pagination etc.

    That’s why we have this hook, so any changes you make this query are done on both autocount, and the results – so everything stays accurate.

    What kind of stuff are you trying to do to your queries using that filter? If it’s anything complex / post meta related you’re right it still might not be efficient, I might have another suggestion depending on your use case.

    Thanks

    Anonymous
    #242096

    I’m trying to add something specific to the meta query, but this addition is done twice because of this.

    Is there a better way to add stuff to the meta query?

    Ross Moderator
    #242101

    No not really – we don’t have much in terms of devs accessing our functions to do DB queries etc our way – so so the sf_edit_query_args is usually the way to go.

    If your meta query requires a meta key to be an exact value, I can provide some code to set this in the query which should be much faster – its a bit clunky (we hope to improve this) but it taps in to our tables for a performance boost.

    Let me know what you’re trying to do (share the snippet of code if you like)

    Thanks

    Anonymous
    #242109

    Hi Ross,

    We are trying to show S&F results on a Google Map.

    Therefore we want to set the posts per page to -1 (I can achieve that).

    But we also want to check the existence of a meta value (the location). For compatibility reasons I add that piece to the meta query (instead of setting the meta value). But maybe I am too careful.

    What I have:

    // modify posts per page to show all
          $query_args['posts_per_page'] = -1;
    
          // only items with location
          $query_args['meta_query'][] = array(
             'key'     => 'fw:opt:location',
          );

    But maybe it could also be:

    // modify posts per page to show all
          $query_args['posts_per_page'] = -1;
    
          // only items with location
          $query_args['meta_query'] = array(
             'key'     => 'fw:opt:location',
          );

    I want to prevent overwriting the meta query if it is already set.

    Ross Moderator
    #242529

    Hi Kevin

    I’m afraid right now I can’t see how we would optimise…

    If you disable “auto count” then you would only have the query run once.

    I’m curious, how many posts are you searching ? If you use a caching plugin, this might be able to save you some resources in the fact the queries shouldn’t actually be run on every page load.

    Thanks

Viewing 8 posts - 1 through 8 (of 8 total)