Forums › Forums › Search & Filter Pro › Include AND exclude category
- This topic has 18 replies, 3 voices, and was last updated 5 years, 9 months ago by Anonymous.
-
Anonymous(Private) January 24, 2019 at 8:57 am #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 WiskundeIn 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(Private) January 28, 2019 at 8:06 pm #200325Hi 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 replace135
with17289
2) The taxonomy name I was using wasquestion_category
, replace this with the name of your taxonomy
3) The part that has theIN
condition, is the equivalent ofinclude
–
'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
Trevor(Private) January 29, 2019 at 11:28 am #200431Ah, 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 );
-
AuthorPosts