我有一个自定义帖子类型,其中有一个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;?>
答案 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
(并移除showposts
,replaced 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',
),
)
);