wp meta_query将在meta_filed乘以时获得没有特定值的帖子

发布于 2025-02-02 19:11:29 字数 524 浏览 2 评论 0原文

找不到一个解决方案如何查询帖子,而没有某些具有许多值的元field值的特定值。

情况是: 当用户执行某些特定操作时,我将在帖子上添加ID。像这样:add_post_meta($ _ post ['post_id'],'users_touched_ids',$ current_user_user-> id);

之后,我必须向他展示并没有被他“触摸过”的用户帖子显示。因此,我正在编写这样的查询:

[
    'relation' => 'OR',
    [
        'key' => 'users_touched_ids',
        'compare' => '!=',
        'value' => $user_id
    ],
    [
        'key' => 'users_touched_ids',
        'compare' => 'NOT EXISTS',
    ],
]

但是它不起作用。我得到所有帖子=(

Can't find a solution how to query posts without some specific value of meta_field that have many values.

The situation is:
I'm adding user id's on posts when they perform some specific operation. Like this: add_post_meta($_POST['post_id'], 'users_touched_ids', $current_user->ID);

After that I have to display to user posts that wasn't "touched" by him. So I'm writing query like this:

[
    'relation' => 'OR',
    [
        'key' => 'users_touched_ids',
        'compare' => '!=',
        'value' => $user_id
    ],
    [
        'key' => 'users_touched_ids',
        'compare' => 'NOT EXISTS',
    ],
]

But it doesn't work. I'm getting all posts =(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2025-02-09 19:11:30

您会收到所有帖子,当查询帖子users_touched_ids不等于$ user_id。这意味着,您将获得所有帖子,其中数据库字段users_touched_ids不包含$ user_id varable。

如果作者与“触摸”用户相同,则可以按照作者过滤查询:

$args = array(
    'author'        =>  $user_id
    'orderby'       =>  'post_date',
    'order'         =>  'ASC',
    'posts_per_page' => 1,
    'meta_query' => [
        [
        'key' => 'users_touched_ids',
        'compare' => 'NOT EXISTS',
        ]
    ]
);

$posts = get_posts( $args );

You getting all posts, while you query posts where users_touched_ids not equals to $user_id. This means, you get all posts where the database field users_touched_ids not contains the $user_id variable.

in case, the Author is the same as the "touch" user, you can filter the query by author like this:

$args = array(
    'author'        =>  $user_id
    'orderby'       =>  'post_date',
    'order'         =>  'ASC',
    'posts_per_page' => 1,
    'meta_query' => [
        [
        'key' => 'users_touched_ids',
        'compare' => 'NOT EXISTS',
        ]
    ]
);

$posts = get_posts( $args );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文