[英]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.