Forums › Forums › Search & Filter Pro › Sticky posts showing up when filtering a category the post is not in
- This topic has 6 replies, 4 voices, and was last updated 1 year, 5 months ago by Anonymous.
-
Anonymous(Private) March 7, 2020 at 10:59 am #235836
Hi
On this site: https://gymogturn.no/nyheter/
We have stickied a post that shows up first, but when we filter on a category the post is not in, we want it to dissapear.
How to do this?
Ross Moderator(Private) March 20, 2020 at 1:07 pm #237362Hi Sigurd
After scratching my head I managed to figure out the issue.
We perform our search outside of the regular
WP_Query
, so if you choose a category in our search form, WP doesn’t actually “know” its restrciting to a specific category, because our search does that, so WP appends the stick post…I’ve managed to come up with some code, that add the WP category to the
WP_Query
(even though our plugin has already restricted the posts to this category), but now WP also knows that at a category restriction is happening, and excludes the sticky:add_filter("sf_edit_query_args", "add_wp_category_to_query", 100, 2); function add_wp_category_to_query($query_args, $sfid){ //if($sfid == 1234){ //you can restrict this behaviour to a search form ID //get user category selection from the search form (ie, detect which categories have been selected) global $searchandfilter; $query_data = $searchandfilter->get($sfid)->current_query(); $query_data_array = $query_data->get_array(); if(isset($query_data_array['_sft_category'])){ $category_values = $query_data_array['_sft_category']['active_terms']; $current_category_ids = array(); //this will contain all the IDs of the categories that have been selected in the search form foreach($category_values as $category){ array_push($current_category_ids, $category['id']); } //restrict query to WP category $query_args['category__in'] = $current_category_ids; } //} return $query_args; }
As there may be some small overhead when implementing this code, you can uncomment the first
if(
line, and restrict this to a specific search form / query.I hope that makes sense & works!
Best
-
AuthorPosts