我有一个活动网站,我想在那里显示可以按升序预订的即将到来的活动,以及按降序关闭注册的活动。
我正在使用get_posts
对于每个查询,虽然我能够列出结果,但似乎无法获得要分页的结果:
$posts = get_posts(
array(
\'post_type\' => \'espresso_event\',
\'meta_key\' => \'event_start_date\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
\'posts_per_page\' => \'10\',
\'paged\' => $paged,
\'meta_query\' => array(
array(
\'key\' => \'event_registration_end\',
\'value\' => date(\'Y-m-d\'),
\'compare\' => \'>=\'
)
)
)
);
$moreposts = get_posts(
array(
\'post_type\' => \'espresso_event\',
\'meta_key\' => \'event_start_date\',
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'posts_per_page\' => \'10\',
\'paged\' => $paged,
\'meta_query\' => array(
array(
\'key\' => \'event_registration_end\',
\'value\' => date(\'Y-m-d\'),
\'compare\' => \'<=\'
)
)
)
);
$posts = array_merge( $posts, $moreposts );
foreach( $posts as $post ) : setup_postdata( $post );
SO网友:Digerkam
首先,您需要将两个查询合并为一个查询,并且需要使用WP\\u query而不是get\\u posts:
$paged = 1 ;
$posts_per_page = 10 ;
$args = array(
\'post_type\' => \'espresso_event\',
\'meta_key\' => \'event_start_date\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
\'posts_per_page\' => $posts_per_page,
\'paged\' => $paged,
\'meta_query\' => array(
array(
\'key\' => \'event_registration_end\',
\'value\' => date(\'Y-m-d\'),
\'compare\' => \'>=\'
),
array(
\'key\' => \'event_registration_end\',
\'value\' => date(\'Y-m-d\'),
\'compare\' => \'<=\'
)
)
) ;
$query = new WP_Query($args) ;
if ($query->have_posts()) {
// Here our parameter:
$total = $query->found_posts ;
while ($query->have_posts()) {
$query->the_post() ;
// Do something like
?><h3><?php the_title() ; ?></h3><?php
}
echo \'<div id="pagination">\' ;
$next_link = \'\' ;
$prev_link = \'\' ;
if ($paged > 1) {
if ($paged-2) {
$prev_link = add_query_arg(array(
\'paged\' => $paged-1,
),get_permalink($post->id)) ;
} else {
$prev_link = get_permalink($post->id) ;
}
}
if (($total/$posts_per_page) > $paged) {
$next_link = add_query_arg(array(
\'paged\' => $paged+1,
),get_permalink($post->id)) ;
}
if ($next_link) {
echo \'<a href="\'.$next_link.\'" class="next">Next Page</a>\' ;
}
if ($prev_link) {
echo \'<a href="\'.$prev_link.\'" class="prev">Previous Page</a>\' ;
}
echo \'</div>\' ;
} else {
echo \'Sorry, there is no post etc\' ;
}
没有测试,但你的需求必须是这样的。