繁体   English   中英

WP meta_query 在 meta_filed 被乘法时获取没有特定值的帖子

[英]WP meta_query to get posts without some specific value when meta_filed is multiply

找不到解决方案如何在没有某些具有许多值的 meta_field 的特定值的情况下查询帖子。

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

之后,我必须向用户显示他没有“触及”的帖子。 所以我写这样的查询:

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

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

当您查询users_touched_ids不等于$user_id的帖子时,您将获得所有帖子。 这意味着,您将获得数据库字段users_touched_ids不包含$user_id变量的所有帖子。

如果作者与“触摸”用户相同,您可以按作者过滤查询,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM