如果将用户添加到帖子,则隐藏帖子WP_QUERY

时间:2019-09-03 作者:Lato

我在wordpress上有这个帖子列表。我制作了一个收藏夹函数,当您将某个内容标记为收藏夹时,它会添加到行的前面。这里的问题是我经营这两个地方。顶部的一个面板显示我最喜欢的帖子,然后是第二个帖子,显示其他用户最喜欢的帖子。顶部面板工作正常。。但问题是,我的个人资料卡显示在第二个面板中,其他用户卡显示在该面板中:https://gyazo.com/75cf45a0844cba1f9ca6818e059d8bf6

正如你所看到的。。。我在第二个面板中有一个副本。如何使用WP\\u查询消除它?

用户位于具有ACF字段的多用户字段中。

我试过做各种各样的事情,比如AND和ORs。我很可能接近解决方案,但我似乎找不到解决方案。。

顶部面板代码:

$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);
第二个面板(下部):

$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);
最后,副本应该隐藏在第二个面板中。我所附的几乎每一张卡片都应该隐藏起来。如果与我的用户无关,带有我最喜欢(星星)的卡应该保留。

1 个回复
SO网友:rozklad

希望我能正确理解你在这里想要实现的目标。

如果要排除所有包含get_current_user_id()project_users ACF中继器,但同时包含hasget_current_user_id() 在里面favourite_users, 正确的元查询如下所示:

\'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\'
        ),
    ),
);
请注意,与您的示例不同,我使用\'relation\' => \'AND\' 因此必须满足这两个条件。

另外,请注意,多个值ACF select字段以序列化方式存储在数据库中。这意味着存储在数据库中的实际值如下所示:

| 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";} |
这意味着compare 应该始终是LIKENOT LIKE 在您的案例中,应该始终引用该值,因为否则总会有数字(s:1表示字符串长度为1个字符等)。另一方面,您正在查找的值(用户ID)将用引号存储。

下面是我最终得到的完整查询:

$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);