0

I try to create a plugin to manage user groups with permission to view posts within group and I stopped because of query problem.

Example: User belong to group called "Group #1" (slug: group-1). I assigned 1 post to "Group #1" and "Group #2" (1 post in 2 terms), 1 post to "Group #2". 1 post is without any group term so it should be visible to all groups because of no assignment. So in the end user should get 2 posts to display.

How to build a wp query (if it's possible) or how to create a sql query to get all post without any term and with terms assign to current user. Any ideas?

1 Answer 1

2

This should be possible with the tax_query parameters for WP_Query.

The query is either for posts where any term NOT EXISTS, OR, for any posts where the term IDs are IN the provided array.

'tax_query' => array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'group',
        'operator' => 'NOT EXISTS',
    ),
    array(
        'taxonomy' => 'group',
        'field'    => 'term_id',
        'terms'    => array( 23, 42 ),
        'operator' => 'IN',
    ),
)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.