Forums Forums Search & Filter Pro Sticky posts showing up when filtering a category the post is not in

Viewing 7 posts - 1 through 7 (of 7 total)
  • Anonymous
    #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?

    Trevor
    #235883

    That would appear to be our standard method of handling ‘sticky’ posts. I will need to refer this to the plugin developer, Ross, to see if there is a way around it.

    Anonymous
    #236266

    Thank you, I’m sure we could do some filter or something.

    Ross Moderator
    #237362

    Hi 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

    Anonymous
    #276810

    I have the same issue as the original author but sadly the function @Ross posted isn’t working and the sticky post is still showing despite being “filtered out”.

    Did something about the plugin change to make that function no longer work?

    Trevor
    #276812

    Could you contact us through our new support system, and please refer to this forum post in your message?

    Anonymous
    #276814

    Will do, thanks Trevor!

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