0

我正在尝试按元值查询帖子类型,但该值是数组中的 [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();

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

4

1 回答 1

1

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

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

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

所以你的 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 数组。

于 2017-10-31T06:09:03.853 回答