最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to do logical OR in terms in WP Query?

programmeradmin4浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论