Wordpress 按数组字段[0]中的PosteTa进行Wp查询

Wordpress 按数组字段[0]中的PosteTa进行Wp查询,wordpress,meta,cmb2,Wordpress,Meta,Cmb2,我试图通过元值查询帖子类型,但该值是数组中的[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

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

显然,它不显示任何帖子、任何想法?

当您将多个选定值存储为元值时,其值以串行形式存储,如

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_查询应如下所示:

$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而不是“Like”来获取帖子,你需要传递“IN”和“IN”值,你需要传递获取帖子的Id数组。

my\u post\u multicheckbox[0]与此有什么关系?为什么在元查询中有
$current\u post\u ID
?您不必
序列化
-WP自动处理-只需输入原始值。我使用CMB2附件post,在post类型“事件”和post类型“位置”之间建立关系。对于每个事件,我都会附上其位置的ID。现在在我的单一地点,我想知道该地点的事件$current_post_ID是该位置的ID,如果事件的附加ID与当前位置的ID相同,则我神奇地拥有我的事件列表!我的想法是通过元值查询事件,其中键是my post meta(my_post_multicheckbox),值是当前位置id。