Home › Forums › General Issues › filter relationship query – exclude if a certain category
I have a relationship field that will show custom posts from two custom post categories, That works fine.
I want to exclude any of these that are also a third custom post category.
I have tried many things.
this is the latest, It still shows on the left side the post that is marked with the third custom post category
add_filter(‘acf/fields/relationship/query/key=field_604935b6dfc02’, ‘s4f_pledgetogoal_query’, 10, 3);
function s4f_pledgetogoal_query( $args, $field, $post_id ) {
$args = array(‘cat’ => ‘-78’);
return $args;
In fullness I have tried setting it as these args 76 and 77 are the categories chosen in the act field filter.
// $args[‘cat’] = array(’76, 77, -78′);
// $args = array(‘cat’ => ’76, 77, -78′);
I have also tried this as a post_object query but it is a relationship field with the result being a post object.
Can anyone tell me where to find out how to do this filter
Thank you
If I understand you want to include posts that are in 2 categories but exclude them if they are also in a third category.
I.E. Show if in category 76 but not if it is also in category 78.
I think that in order to do this you need to use a tax_query. I could be wrong but I do not think the ‘cat’ argument can be used the way you are trying to use it and I also thing that using ‘category__in’ combined with ‘category__not_in’ will work the same way. Like I said, I could be wrong but I think these arguments work as ‘OR’ (meaning category 76 OR not category 78).
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id'
'terms' => array(76, 77),
'operator' => 'IN'
),
array(
'taxonomy' => 'category',
'field' => 'term_id'
'terms' => array(78),
'operator' => 'NOT IN'
)
);
That was great, Thank you very much.
For others, change the taxonomy to the cpt category if needed, and add a comma after the ‘field’ => ‘term_id’ in the array statements and of course change the category id to what you want.
T
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.