Forums Forums Search & Filter Pro Include AND exclude category

Viewing 9 posts - 11 through 19 (of 19 total)
  • Ross Moderator
    #199853
    This reply has been marked as private.
    Anonymous
    #199876

    ok ross, sorry i will explain with three posts

    Let’s say we have:
    -First post having the categories Aardrijkskunde, Biologie and Wiskunde
    -The second post ia having the categories Aardrijkskunde, Biologie. Wiskunde en Archief
    -The third post is having the categories Biologie and Wiskunde

    In the mentioned filter results I want to include in the results all posts having the category Aardrijkskunde but I also want to exlude the posts having the category Archief (post 2).
    So in the results only the first post should be shown.

    In the settings of the filter I can include only Aardrijkkunde, but then the post having the category Archief will also be shown (which i do not want).
    If I exclude only Archief, the third post will be shown in the results, which i don’t want because it does not have Aardrijkskunde.
    If I exlude Archief, Biologie and Wiskunde, there are no results at all, and the desired first post is also not shown.

    Hope it is clear now to you

    Ross Moderator
    #200325

    Hi Jeroen

    Thanks for your patience, I finally understood the use case!

    I had a little think about this, my first issue was I thought a WP_Query couldn’t do this, because how would it know which rule takes precedent..?

    Anyway, turns out it is possible, which mean you can use our filter (sf_edit_query_args) to do this programmatically. This worked for me locally (you’ll have to replace some variables):

    //include and exclude a tax / category
    function include_exclude_search_filter( $query_args, $sfid ) {
    	
    	//if search form ID = 135, the do something with this query
    	if($sfid==135)
    	{
    		//modify $query_args here before returning it
    		$query_args['tax_query'] = array(
    		
    			'relation' => 'AND',
    			array(
    				'taxonomy' => 'question_category',
    				'field'    => 'term_id',
    				'terms'    => array( 254 ),
    				'operator' => 'IN',
    			),
    			array(
    				'taxonomy' => 'question_category',
    				'field'    => 'term_id',
    				'terms'    => array( 253 ),
    				'operator' => 'NOT IN',
    			)
    		);
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'include_exclude_search_filter', 20, 2 );

    Make sure to add this to your functions.php

    Things that need to be replaced in the above code:
    1) at the top you need to replace the search form ID with your own, so replace 135 with 17289
    2) The taxonomy name I was using was question_category, replace this with the name of your taxonomy
    3) The part that has the IN condition, is the equivalent of include
    'terms' => array( 254 ),
    So change 254 for the ID you want to include.
    4) The next part is the exclude:
    'terms' => array( 253 ),
    So change 253 for the ID of the category you want to exclude.

    If you need to know the IDs of your categories/taxonomies, just go to the edit page and check the URL for the ID.

    And thats it! You should be good to go, let me know how you get on.

    Thanks

    Anonymous
    #200417

    Hi ross, thanks so much for taking time for this!
    It works for this form. Now i just have to implement that for all the forms 🙂
    thanks again!!

    Anonymous
    #200419

    Sorry I have to come back: if I copy the code in the functions.php and re-use it on another form, I get 500 error. Any idea what to do?
    thanks again

    Trevor
    #200421

    Did you put the whole function in twice (but edited)?

    Anonymous
    #200427

    Hi trevor, yesd I copied the whole function, and changed the id’s, then the 500 error appeared

    Trevor
    #200431

    Ah, try this type of method then:

    function include_exclude_search_filter( $query_args, $sfid ) {
    	
    	//if search form ID = 135, the do something with this query
    	if($sfid==135)
    	{
    		//modify $query_args here before returning it
    		$query_args['tax_query'] = array(
    		
    			'relation' => 'AND',
    			array(
    				'taxonomy' => 'question_category',
    				'field'    => 'term_id',
    				'terms'    => array( 254 ),
    				'operator' => 'IN',
    			),
    			array(
    				'taxonomy' => 'question_category',
    				'field'    => 'term_id',
    				'terms'    => array( 253 ),
    				'operator' => 'NOT IN',
    			)
    		);
    	}
        elseif($sfid==1234)
        {
            more stuff
        }
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'include_exclude_search_filter', 20, 2 );
    Anonymous
    #200437

    Great, thanks!!

Viewing 9 posts - 11 through 19 (of 19 total)