数组字段[0]中postmeta的Wp查询
Wp query by postmeta in array field [0]
我正在尝试通过元值查询 post 类型,但该值是数组中的 [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();
显然它没有显示任何内容 post,有什么想法吗?
当您将多个选定值存储为元值时,其值将以序列化形式存储,如
a:2:{i:0;s:2:"53";i:1;s:2:"54";} // where 53 and 54 are 2 selected ids.
现在,如果您想获得选择了 id = 53 的 post,那么您需要在元查询中使用 "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 获取 post,而不是 'Like',你需要传递 'IN' 并且在值中,你需要传递 id 数组想要得到 posts.
我正在尝试通过元值查询 post 类型,但该值是数组中的 [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();
显然它没有显示任何内容 post,有什么想法吗?
当您将多个选定值存储为元值时,其值将以序列化形式存储,如
a:2:{i:0;s:2:"53";i:1;s:2:"54";} // where 53 and 54 are 2 selected ids.
现在,如果您想获得选择了 id = 53 的 post,那么您需要在元查询中使用 "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 获取 post,而不是 'Like',你需要传递 'IN' 并且在值中,你需要传递 id 数组想要得到 posts.