自定义帖子类型的所有帖子的WP循环特定元代码值

时间:2014-04-29 08:41:27

标签: php wordpress

我已经从自定义帖子类型的所有帖子创建了特定于循环的元数据值的函数。它工作得很好。以下是代码:

$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"

我该怎么做?

1 个答案:

答案 0 :(得分:-1)

使用meta_query

$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;