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

php - Hide posts if user is added to it WP_query

programmeradmin0浏览0评论

I have this list of posts in wordpress. I've made a favorite function that when you mark something as favorite it gets added to the front of the line. The issue here is I run this 2 place. One top panel which shows my favorite posts that I'm attached to and then the second post that shows my favorized posts from other users. Top panel works just fine.. But the issue is that my profile cards are shown in the second panel where other users cards are shown:

As you can see... I have a duplicate in the second panel. How do I get rid of it using WP_Query?

Users are inside a multiple user field with ACF Fields.

I've tried doing all kinds of stuff such as AND and ORs. I am mostly likely close to the solution, but I can't seem to find the solution..

TOP Panel code:

$aProjectArgs = array(
        'post_type' => 'project',
        'post_status'=> 'publish',
        'posts_per_page'=> -1,
        'orderby'   => 'favorit_users',
        'order'   => 'DESC',
        'meta_query' => array(
            'relation' => 'OR',
                array(
                    'relation' => 'OR',
                    array(
                        'key' => 'project_users',
                        'value' => get_current_user_id(),
                        'compare' => '='
                    ),
                    array(
                        'key' => 'project_users', // name of custom field
                        'value' => '"'.get_current_user_id().'"',
                        'compare' => 'LIKE'
                    ),
                ),
                array(
                    'relation' => 'AND',
                    array(
                        'relation' => 'OR',
                        array(
                            'key' => 'project_users',
                            'value' => get_current_user_id(),
                            'compare' => '='
                        ),
                        array(
                            'key' => 'project_users', // name of custom field
                            'value' => '"'.get_current_user_id().'"',
                            'compare' => 'LIKE'
                        ),
                    ),
                    "favorit_users" => array(
                        'key' => 'favorite_users',
                        'value' => get_current_user_id(), 
                        'compare' => 'LIKE'
                    ),
                ),
            ),
        );
    $aProjectQuery = new WP_Query($aProjectArgs);

Second panel (LOWER):

$allProjectArgs = array(
        'post_type' => 'project',
        'post_status'=> 'publish',
        'posts_per_page'=> -1,
        'orderby'   => 'favorit_users',
        'order'   => 'DESC',
        'meta_query' => array(
            'relation' => 'OR',
                array(
                    'relation' => 'OR',
                    array(
                        'key' => 'project_users',
                        'value' => get_current_user_id(),
                        'compare' => '!='
                    ),
                    array(
                        'key' => 'project_users',
                        'value' => '"'.get_current_user_id().'"',
                        'compare' => 'NOT LIKE'
                    ),
                    array(
                        'key' => 'favorite_users',
                        'compare' => 'NOT EXISTS'
                    ),
                ),
                "favorit_users" => array(
                    'key' => 'favorite_users',
                    'value' => $iCurrentUserID, 
                    'compare' => 'LIKE'
                ),
            ),
        );
    $allProjectQuery = new WP_Query($allProjectArgs);

In the end the duplicate should be hidden in the second panel. Pretty much every card I am attached to should be hidden. Cards with my favorite (stars) should stay IF it is not related to my user.

I have this list of posts in wordpress. I've made a favorite function that when you mark something as favorite it gets added to the front of the line. The issue here is I run this 2 place. One top panel which shows my favorite posts that I'm attached to and then the second post that shows my favorized posts from other users. Top panel works just fine.. But the issue is that my profile cards are shown in the second panel where other users cards are shown: https://gyazo/75cf45a0844cba1f9ca6818e059d8bf6

As you can see... I have a duplicate in the second panel. How do I get rid of it using WP_Query?

Users are inside a multiple user field with ACF Fields.

I've tried doing all kinds of stuff such as AND and ORs. I am mostly likely close to the solution, but I can't seem to find the solution..

TOP Panel code:

$aProjectArgs = array(
        'post_type' => 'project',
        'post_status'=> 'publish',
        'posts_per_page'=> -1,
        'orderby'   => 'favorit_users',
        'order'   => 'DESC',
        'meta_query' => array(
            'relation' => 'OR',
                array(
                    'relation' => 'OR',
                    array(
                        'key' => 'project_users',
                        'value' => get_current_user_id(),
                        'compare' => '='
                    ),
                    array(
                        'key' => 'project_users', // name of custom field
                        'value' => '"'.get_current_user_id().'"',
                        'compare' => 'LIKE'
                    ),
                ),
                array(
                    'relation' => 'AND',
                    array(
                        'relation' => 'OR',
                        array(
                            'key' => 'project_users',
                            'value' => get_current_user_id(),
                            'compare' => '='
                        ),
                        array(
                            'key' => 'project_users', // name of custom field
                            'value' => '"'.get_current_user_id().'"',
                            'compare' => 'LIKE'
                        ),
                    ),
                    "favorit_users" => array(
                        'key' => 'favorite_users',
                        'value' => get_current_user_id(), 
                        'compare' => 'LIKE'
                    ),
                ),
            ),
        );
    $aProjectQuery = new WP_Query($aProjectArgs);

Second panel (LOWER):

$allProjectArgs = array(
        'post_type' => 'project',
        'post_status'=> 'publish',
        'posts_per_page'=> -1,
        'orderby'   => 'favorit_users',
        'order'   => 'DESC',
        'meta_query' => array(
            'relation' => 'OR',
                array(
                    'relation' => 'OR',
                    array(
                        'key' => 'project_users',
                        'value' => get_current_user_id(),
                        'compare' => '!='
                    ),
                    array(
                        'key' => 'project_users',
                        'value' => '"'.get_current_user_id().'"',
                        'compare' => 'NOT LIKE'
                    ),
                    array(
                        'key' => 'favorite_users',
                        'compare' => 'NOT EXISTS'
                    ),
                ),
                "favorit_users" => array(
                    'key' => 'favorite_users',
                    'value' => $iCurrentUserID, 
                    'compare' => 'LIKE'
                ),
            ),
        );
    $allProjectQuery = new WP_Query($allProjectArgs);

In the end the duplicate should be hidden in the second panel. Pretty much every card I am attached to should be hidden. Cards with my favorite (stars) should stay IF it is not related to my user.

Share Improve this question asked Sep 3, 2019 at 11:36 LatoLato 1
Add a comment  | 

1 Answer 1

Reset to default 1

Hopefully i understood correctly what you want to achieve here.

If you want to exclude all posts that contain get_current_user_id() in the project_users ACF repeater, but at the same time contains has get_current_user_id() in favourite_users, the correct meta query looks like this:

'meta_query' => array(
    'relation' => 'AND',
        array(
            array(
                'key' => 'project_users',
                'value' => '"'.get_current_user_id().'"',
                'compare' => 'NOT LIKE'
            ),
        ),
        "favorit_users" => array(
            'key' => 'favorite_users',
            'value' => '"'.get_current_user_id().'"',
            'compare' => 'LIKE'
        ),
    ),
);

Note that unlike in your example, i'm using 'relation' => 'AND' so that both conditions have to be met.

Also, note that multiple values ACF select fields are stored serialized in the db. That means the real value stored in the database looks like this:

| meta_id | post_id | meta_key      | meta_value         |
|---------|---------|---------------|--------------------|
| 14      |       8 | project_users | a:1:{i:0;s:1:"1";} |
| 20      |      11 | project_users | a:1:{i:0;s:1:"2";} |

Which means that the compare should always be LIKE or NOT LIKE and the value should always be quoted in your case, because there will always be numbers otherwise (s:1 for string 1 character long etc.). The values you are looking for (user ids) on the other hand will be stored with quotes.

Here is the full query i ended up with:

$allProjectArgs = array(
    'post_type' => 'project',
    'post_status'=> 'publish',
    'posts_per_page'=> -1,
    'orderby'   => 'favorit_users',
    'order'   => 'DESC',
    'meta_query' => array(
        'relation' => 'AND',
            array(
                array(
                    'key' => 'project_users',
                    'value' => '"'.get_current_user_id().'"',
                    'compare' => 'NOT LIKE'
                ),
            ),
            "favorit_users" => array(
                'key' => 'favorite_users',
                'value' => '"'.get_current_user_id().'"',
                'compare' => 'LIKE'
            ),
        ),
    );
$allProjectQuery = new WP_Query($allProjectArgs);
发布评论

评论列表(0)

  1. 暂无评论