简体   繁体   English

在数组字段[0]中通过postmeta进行WP查询

[英]Wp query by postmeta in array field [0]

I'm trying to query a post type by meta value, but the value is [0] field in array. 我正在尝试通过元值查询帖子类型,但该值是数组中的[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();

Obviously it does not show any post, any idea? 显然它不显示任何帖子,有什么想法吗?

When you store multiple selected value as meta value, its value are being store in serialise form like 当您将多个选定值存储为元值时,其值将以序列化形式存储,例如

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

Now, if you want to get post which has selected id = 53, then you need to pass "compare" parameter in your meta query with "Like". 现在,如果要获取已选择id = 53的帖子,则需要在元查询中使用“赞”传递“比较”参数。 By default it will compare with "=" condition. 默认情况下,它将与“ =”条件进行比较。

So your Wp_query should be as follow: 因此,您的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);

If you want to fetch post by multiple Id, instead of 'Like', you need to pass 'IN' and in value, you need to pass array of ids by which you want to get the posts. 如果要按多个ID而不是“ Like”来获取帖子,则需要传递“ IN”和值,则需要传递要获取其帖子的ID数组。

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

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