How to do logical OR in terms in WP Query?

I have a code that hides images of other users. But I need to make sure that users can see their images OR images from c

I have a code that hides images of other users. But I need to make sure that users can see their images OR images from categories.

function my_files_only( $wp_query ) {

    if ( ! $_POST["action"] == "attachment" )
    {
            return;
    }

    if ( current_user_can( 'image_verified' ) )
    {
            return;
    }

    global $current_user;
    $wp_query->set( 'author', "$current_user->id"); }
    add_filter('parse_query', 'my_files_only' );

You can also add: $wp_query->set( 'mlo-category', "license");

But then the selection logic will be equal to AND. Therefore, we need to prescribe: relation OR. How can I fit such a request in my code?

I have a code that hides images of other users. But I need to make sure that users can see their images OR images from categories.

function my_files_only( $wp_query ) {

    if ( ! $_POST["action"] == "attachment" )
    {
            return;
    }

    if ( current_user_can( 'image_verified' ) )
    {
            return;
    }

    global $current_user;
    $wp_query->set( 'author', "$current_user->id"); }
    add_filter('parse_query', 'my_files_only' );

You can also add: $wp_query->set( 'mlo-category', "license");

But then the selection logic will be equal to AND. Therefore, we need to prescribe: relation OR. How can I fit such a request in my code?

Share Improve this question edited Jul 9, 2020 at 18:49 mozboz 2,6281 gold badge12 silver badges23 bronze badges asked Jul 9, 2020 at 13:10 Mark DidleMark Didle 1 2
  • Just so I got it right, you want a query that does author = current_user OR mlo-category = "license" ? – mozboz Commented Jul 9, 2020 at 18:56
  • Yes, it's right! – Mark Didle Commented Jul 9, 2020 at 19:50
Add a comment  | 

1 Answer 1

Reset to default 1

As WP_Query doesn't natively allow you to do logical OR operations (except on meta values), one option is just to run two queries to get all the Post ID's, then pass those to a new WP_Query which you can use for a loop. This is quite long and requires 3 queries, so perhaps someone will know a shorter solution

This code taken from an example here, and untested. You may have to tweak it for your use case, in particular I'm not sure what 'mlo-category' is, so you may need to convert that to a better WP_Query parameter

    $postsCurrentUser = get_posts(array(
        'author', "$current_user->id"); 
        ));
        
    $postsCategory = get_posts(array(
        'mlo-category', "license"
        ));

    $mergedPosts = array_merge( $postsCurrentUser, $postsCategory ); //combine queries

    $postIds = array();

    // Find any post ID that is in one array or the other, don't include any twice
    foreach( $mergedPosts as $item ) {
        if (!in_array($item->ID, $postIds)) {
            $postIds[]=$item->ID;
        }
    }

    $args = array('post__in' => $postIds);

    $q = WP_Query($args);

Let me know if that does what you want

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742277793a4413915.html

相关推荐

  • How to do logical OR in terms in WP Query?

    I have a code that hides images of other users. But I need to make sure that users can see their images OR images from c

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信