我已经从自定义帖子类型的所有帖子创建了特定于循环的元数据值的函数。它工作得很好。以下是代码:
$loop = new WP_Query(array('post_type' => 'myposttype', 'posts_per_page' => -1));
while ( $loop->have_posts() ) : $loop->the_post();
$custom_fields = get_post_custom($post->ID);
$my_custom_field = $custom_fields['custommetabox_mb'];
foreach ( $my_custom_field as $key => $value ) {
echo $value . "<br />";
}
endwhile;
是否可以循环一些元数据值。例如:
此自定义帖子类型上有2个元数据块。
First = custommetabox_mb
Second = custommetabox_mbb
我想要的是,如果custommetabox_mb
值与文字custommetabox_mbb
相同,则只需循环"yiedpozi"
。
我该怎么做?
答案 0 :(得分:-1)
$args = array('post_type' => 'myposttype', 'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'custommetabox_mb',
'value' => 'yiedpozi',
'compare' => '='
),
array(
'key' => 'custommetabox_mbb',
'value' => 'yiedpozi',
'compare' => '='
)
), 'posts_per_page' => -1,);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post();
$custom_fields = get_post_custom($post->ID);
$my_custom_field = $custom_fields['custommetabox_mb'];
foreach ( $my_custom_field as $key => $value ) {
echo $value . "<br />";
}
endwhile;