dongyukui8330 2016-01-11 19:05
浏览 165

将两个不同的查询合并为一个并对查询进行分页 - wordpress

Basically, I want to show the items that have the tag "Featured" first, then show all the items that do not have the tag "Featured, and then paginate the results.

$featured = array(
'post_type' => 'aircraft-refueling',
'tag' => 'featured'
);
$not_featured = array(
'post_type' => 'aircraft-refueling',
'tag__not_in' => array('592')
);

Here is the pagination:

function pagination()
{
global $wp_query;
$big = 999999999;
echo paginate_links(array(
    'base' => str_replace($big, '%#%', get_pagenum_link($big)),
    'format' => '?paged=%#%',
    'current' => max(1, get_query_var('paged')),
    'total' => $wp_query->max_num_pages
));
}

I am not too familiar with PHP, but I am guessing that I need to merge the queries into a new query and then paginate that new query? Help is highly appreciated! If there is a better method, please advise as well.

EDIT: Here is my progress. I almost got it to work but I am getting an error with this code even though it looks fine to me...?

<?php 
$featured = get_posts(array(
    'post_type' => 'aircraft-refueling',
    'tag' => 'featured'
    ));
$notfeatured = get_posts(array(
    'post_type' => 'aircraft-refueling',
    'tag__not_in' => array('592')
    ));
$mergedposts = array_merge( $featured, $notfeatured );

$postids = array();
foreach( $mergedposts as $item ) {
$postids[]=$item->ID;
}
$uniqueposts = array_unique($postids);

$posts = get_posts(array(
    'post__in' => $uniqueposts,
    'post_type' => 'aircraft-refueling',
    'post_status' => 'publish'
    ));
foreach( $posts as $post ) :
setup_postdata($post);
?>

Then the standard query:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// THE CONTENT
<?php endwhile; ?>
<?php endif; ?>
  • 写回答

1条回答 默认 最新

  • 普通网友 2016-01-12 09:44
    关注

    From provided code above I can't figure how you run the loop which display data. If you did:

    <?php foreach( $posts as $post ) : 
         setup_postdata($post); 
    endforeach; ?>
    

    then there is no need from second loop:

    while ( have_posts() ) : the_post(); ?>
    // THE CONTENT
    <?php endwhile; ?>
    

    because setup_postdata() and the_post() did basically the same - they setup global $post object with data from the loop so template tags like the_title(), the_ID() .. can work propertly.

    So you must do:

    If you use query_posts()

    foreach ($posts as $post):
      setup_postdata($post) 
     //THE CONTENT (the_title(), the_ID() will work 
    endforeach;
    

    or if you use WP_Query() - I think this is the best way

    $posts = new WP_Query(array(
        'post__in' => $uniqueposts,
        'post_type' => 'aircraft-refueling',
        'post_status' => 'publish'
    ));
    
    if( $posts->have_posts()):
        while($posts->have_posts()): $posts->the_post()
        //THE CONTENT (the_title(), the_ID() will work 
        endwhile; 
    endif;
    

    In both cases you must do wp_reset_postdata() after the loop

    In many cases if you did query with custom_post_types this broke the pagination :) If this is the case: css-tricks

    And last: did you need this array_unique check? The post or have or have not this tag...

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大