get_posts 对用户关系 (ACF) 的元查询未返回任何结果

get_posts with meta query for a user relation (ACF) not returning any result

我有一个自定义 post 类型 fvc_members

使用高级自定义字段,我添加了一个名为 user 的新字段,它是现有用户的关系对象。

我有以下代码来查询当前用户设置为 user

的 posts
$args = array(
    'numberposts' => 5,
    'post_type' => 'fvc_members',
    'meta_query' => array(
        array(
            'key'       => 'user',
            'value'     => '"' . get_current_user_id() . '"',
            'compare'   => '='
        )
    )
);

$posts = get_posts($args);

我尝试了不同的选项来写入值(包裹在 '' 中,包裹在 "" 中,不包裹,与 LIKE 比较),但是我找不到使它工作的方法

If user has only single integer value then you dont need the inverted comma, and also add the compare type.

代码如下:

$args = [
    //...
    //...
    'meta_query' => [
        [
            'key' => 'user',
            'value' => get_current_user_id(),
            'compare' => '=',
            'type' => 'NUMERIC' //<-- add this
        ]
    ]
];
//...
$posts = get_posts($args);

参考:Custom Field Parameters

希望对您有所帮助!

尝试 $args 而不使用 compare 语句。您只需要使它自动成为 "compare" 语句的键和值:

$args = array(
    'numberposts' => 5,
    'post_type' => 'fvc_members',
    'meta_query' => array(
        array(
            'key'       => 'user',
            'value'     => '"' . get_current_user_id() . '"',
        )
    )
);