繁体   English   中英

查询按 ACF 用户 id 过滤的自定义帖子

[英]Query custom posts filtered by ACF users id

我有一个名为journals的自定义帖子类型和一个名为editors的 ACF Users字段(返回格式 User ID )。

在添加新期刊时,您可以 select 多个用户作为编辑。

现在,我想创建一个查询来获取当前用户被指定为编辑的所有期刊。

我试过这样的事情:

$current_user = wp_get_current_user();

$args = array(  
  'post_type' => 'journals',
  'post_status' => 'publish',
  'meta_query' => array(
     array(
       'key' => 'editors',
       'value' => $current_user->ID, 
       'compare' => 'IN'
     )
   )
);

我怎样才能做到这一点?

我使用LIKE而不是IN如下 -

$current_user = wp_get_current_user();

$args = array(  
    'post_type' => 'journals',
    'post_status' => 'publish',
    'meta_query' => array(
        array(
            'key' => 'editors',
            'value' => $current_user->ID,
            'compare' => 'LIKE'
        )
    )
);

这对我有用。

星期天 Lalbiaknia 是正确的。 但在我这边,由于 $current_user->ID 是 integer,因此对此进行了轻微修改,您可以这样做(注意分号):

$current_user = wp_get_current_user();

$args = array(  
    'post_type' => 'journals',
    'post_status' => 'publish',
    'meta_query' => array(
        array(
            'key' => 'editors',
            'value' => $current_user->ID . ';',
            'compare' => 'LIKE'
        )
    )
);

暂无
暂无

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

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