Wp_query by post meta in array field [0]

问题描述 投票:0回答:1

我正在尝试按元值查询帖子类型,但值为数组中的[0]字段。

$args = array('post_type'=>'event','meta_query' => array(
    array(
        'key' => 'my_post_multicheckbox', 
         // The right value should be my_post_multicheckbox[0] 
         // and it's serialized
        'value' => serialize($current_post_ID)
    )
));
$events = new WP_Query($args);
while ($events->have_posts()):$events->the_post(); 
the_title();
endwhile;
wp_reset_query();

显然它没有显示任何帖子,任何想法?

wordpress meta cmb2
1个回答
1
投票

当您将多个选定值存储为元值时,其值将以序列形式存储

a:2:{i:0;s:2:"53";i:1;s:2:"54";} // where 53 and 54 are 2 selected ids.

现在,如果您想获得选择了id = 53的帖子,那么您需要将元查询中的“compare”参数传递给“Like”。默认情况下,它将与“=”条件进行比较。

所以你的Wp_query应该如下:

$args = array('post_type'=>'event','meta_query' => array(
    array(
        'key' => 'my_post_multicheckbox', 
         // The right value should be my_post_multicheckbox[0] 
         // and it's serialized
        'value' => $current_post_ID, // this should not be serialise value.
        'type' => 'CHAR',
        'compare' => 'LIKE',
    )
));
$events = new WP_Query($args);

如果你想通过多个Id获取帖子,而不是'喜欢',你需要传递'IN'并且在值中,你需要传递你想要获得帖子的id数组。

© www.soinside.com 2019 - 2024. All rights reserved.