WP_Query在一个页面上两次 - 没有获得每页所需的帖子

时间:2016-02-03 14:56:26

标签: php wordpress custom-post-type

我有一个自定义帖子类型,其中有一个ACF复选框,用于定义帖子是否有特色。我想在页面上显示6个精选帖子和6个非特色帖子,因此创建了两个带有单独args的WP_Query循环。虽然特色和非特色的分离工作,但页面总共只显示了6个帖子,我不知道如何解决这个问题?

我的代码:

        <?php
            $args1 = array(
            post_type       => 'fairs',
            posts_per_page  =>  -1,
            showposts       => 6 ,
            meta_key        => 'start',
            orderby         => 'meta_value_num',
            order           => 'ASC'

            );
            $new1 = new WP_Query($args1);?>
            <?php while ($new1->have_posts()) : $new1->the_post();?>
                <?php $field = get_field( 'wa_feature', get_the_ID() ); if ( true == $field ):?>   
                    Featured Posts
                <?php endif;?>
            <?php endwhile;  wp_reset_postdata();?>

        <?php
            $args2 = array(
            post_type       => 'fairs',
            posts_per_page  =>  -1,
            showposts       => 6 ,
            meta_key        => 'start',
            orderby         => 'meta_value_num',
            order           => 'ASC'

            );
            $new2 = new WP_Query($args2);?>
            <?php while ($new2->have_posts()) : $new2->the_post();?>
                <?php $field = get_field( 'wa_feature', get_the_ID() ); if ( false == $field ):?>   
                    Non-featured Posts
               <?php endif;?>
            <?php endwhile;?>

2 个答案:

答案 0 :(得分:0)

你的论点应该引用:

array(
    'post_type'       => 'fairs',
    'posts_per_page'  => 6,
    'meta_key'        => 'start',
    'orderby'         => 'meta_value_num',
    'order'           => 'ASC'
);

数组键可以是整数或字符串。请阅读PHP array() docs

中的更多内容

在这种情况下,您也应该只使用posts_per_page(并移除showpostsreplaced by posts_per_page in WP 2.1

答案 1 :(得分:0)

您的WP_Query不对。您执行两个完全相同的查询($args1 = $args2),并期望得到不同的结果。您还将meta_key放入查询参数中,但不知道比较的内容和方式。

根据您选择的类型,正确的语法,如果您的元文件名为featured且查询可以是:

args1 = array(
  'post_type'       => 'fairs',
  'posts_per_page'  =>  -1,
  'meta_key'        => 'featured',
  'meta_value'      => true //or 1, or 'yes` depending of ACF type you have choose 
  'orderby'         => 'meta_value_num',
  'order'           => 'ASC'
);

args1 = array(
  'post_type'       => 'fairs',
  'posts_per_page'  =>  -1,
  'meta_key'        => 'featured',
  'meta_value'      => false//or 0, or 'no` 
  'orderby'         => 'meta_value_num',
  'order'           => 'ASC'
);

或者此featured字段仅存在于featured帖子中

args1 = array(
  'post_type'       => 'fairs',
  'posts_per_page'  =>  -1,
  'orderby'         => 'meta_value_num',
  'order'           => 'ASC'.
  'meta_query' => array(
     array(
        'key'     => 'featured',
        'compare' => 'EXISTS',
     ),
   )
);

args1 = array(
  'post_type'       => 'fairs',
  'posts_per_page'  =>  -1,
  'orderby'         => 'meta_value_num',
  'order'           => 'ASC'.
  'meta_query' => array(
     array(
        'key'     => 'featured',
        'compare' => 'NOT EXISTS',
     ),
   )
);

检查ACF queryCodex是否有正确的sintax。