Forums › Forums › Search & Filter Pro › Count of filters not correct after using pre_get_posts
Tagged: V3
- This topic has 7 replies, 3 voices, and was last updated 5 years, 11 months ago by
Ross.
-
Anonymous(Private) April 15, 2020 at 7:57 pm #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!
-KevinTrevor(Private) April 16, 2020 at 12:56 pm #240256Ideally, 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
Ross Moderator(Private) April 30, 2020 at 9:22 am #242066Hi 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__inagument of twoWP_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
Ross Moderator(Private) April 30, 2020 at 11:18 am #242101No 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(Private) April 30, 2020 at 11:35 am #242109Hi 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(Private) May 4, 2020 at 3:51 pm #242529Hi 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
-
AuthorPosts