【问题标题】:One pagination for multiple queries (or how to group a one query)多个查询的一个分页(或如何对一个查询进行分组)
【发布时间】:2017-01-19 20:46:49
【问题描述】:

我在同一页面中有多个查询,如下所示:

$args_1 = array (
    'category_name' => get_query_var( 'category_name' ),
    'post__in'      => $sticky = get_option( 'sticky_posts' );
    'orderby' => 'date',
    'order' => 'DESC'    
);

$sticky_query = new WP_Query ($args_1);

// loop


$args_2 = array(
    'category_name' => get_query_var( 'category_name' ),
    'post__not_in'  => get_option( 'sticky_posts' ),
    'category__not_in' => array(11114),
    'orderby' => 'date',
    'order' => 'DESC'
);

$query_2 = new WP_Query ($args_2);

// loop



$args_3 = array(
    'category_name' => get_query_var( 'category_name' ),
    'post__not_in'  => get_option( 'sticky_posts' ),
    'category__in' => array(11114),
    'orderby' => 'date',
    'order' => 'DESC'
);


$query_3 = new WP_Query($args_3 );

// loop

我愿意:

1) 将每页的帖子总数限制为 15 个

2) (我做不到,我在任何地方都搜索过,但没有找到解决方案)让这个“组合”分页工作。现在,这是合乎逻辑的,第二页与第一页相同......

或者解决方案可以是只进行一次查询并将帖子分组并改为对组进行排序?

【问题讨论】:

标签: wordpress pagination


【解决方案1】:

最终解决方案

  1. 如果合并两个查询并且不使用“偏移量”,则不需要自定义分页,因此我已删除

    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big  ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $the_query->max_num_pages
    ) );
    

并补充:

    echo paginate_links(); 
  1. 您必须将 'order' 设置为 'post__in' 值以保持在单独查询中给帖子的顺序。

  2. 为避免由包含许多帖子的查询引起的内存错误(如“致命错误:允许的内存大小为 134217728 字节已用尽(尝试分配 262144 字节)”),您必须仅检索帖子 ID在查询中。 See here.

这是最终代码:

    global $wp_query;



    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $post_per_page = 35; // How many post per page - setup as you need
    $nr_of_posts_to_be_analyzed_to_include_only_current_events = 100; // How many post to analyze to include only current events, ie events that are not in category id 11114



    $sticky = get_option( 'sticky_posts' );

    // posts in category, limited in number otherwise - if you set '-1' and you have 7.000 posts - you exceed the script time limit execution 
    $category_posts = get_posts(array('category_name' => get_query_var( 'category_name' ), 'posts_per_page' => $nr_of_posts_to_be_analyzed_to_include_only_current_events,'orderby' => 'date', 'order' => 'DESC'));

    $category_posts_ids = array();

    foreach( $category_posts as $post ) {

        $category_posts_ids[]=$post->ID; // Array with posts ID

    }
    // var_dump($category_posts_ids);


   // expired posts in category, limited in number otherwise - if you set '-1' and you have 7.000 posts - you exceed the script time limit execution 
   $expired_posts = get_posts(array('category__and' => array(11114, get_query_var('cat')), 'posts_per_page' => $nr_of_posts_to_be_analyzed_to_include_only_current_events, 'orderby' => 'date', 'order' => 'DESC'));

   $expired_posts_ids = array();

   foreach( $expired_posts as $post ) {

        $expired_posts_ids[]=$post->ID; // Array with posts ID

   }



   // first 100 posts in this category per page - the expired posts that are in 100 first posts in this category gives as result an array of not expired posts
   $array_where_to_get_post =  array_diff( $category_posts_ids, $sticky, $expired_posts_ids );



   // 1/3 only the sticky posts
   $stickies_posts_args = array(
    'category_name' => get_query_var( 'category_name' ),
    'post__in'  => get_option( 'sticky_posts' ),
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => -1,
    'fields'         => 'ids' // important: to avoid memory exhausted error we retrieve postids only
    );

   // 2/3 only the not expired and not sticky posts
   $not_expired_posts_args = array(
    'post__in' => $array_where_to_get_post,
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => -1,
    'fields'         => 'ids', // important: to avoid memory exhausted error we retrieve postids only,
    'suppress_filters' => false 
   );

   // 3/3 only the expired and not sticky posts
   $expired_posts_args = array(
    'category_name' => get_query_var( 'category_name' ),
    'post__not_in'  => get_option( 'sticky_posts' ), // not sticky posts
    'category__in' => array(11114), // expired posts
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => -1,
    'fields'         => 'ids' // important: to avoid memory exhausted error we retrieve postids only
   );


  $firstQuery = get_posts($stickies_posts_args);

  add_filter('posts_join','join_posts_and_events'); // apply some filter
  add_filter('posts_orderby', 'order_posts'); // apply some filter
  $secondQuery = get_posts($not_expired_posts_args);
  remove_filter('posts_join','join_posts_and_events');
  remove_filter('posts_orderby', 'order_posts');

  $thirdQuery = get_posts($expired_posts_args);


  $mergePosts = array_merge( $firstQuery, $secondQuery, $thirdQuery ); // Merge all queries

  $uniquePosts = array_unique($mergePosts); // Create an array with unique posts

   // Final query
   $args = array(
    'post_type' => 'any',   
    'post__in' => $uniquePosts,
    'paged' => $paged,
    'orderby' => 'post__in', // order the posts as they are (already ordered)
    'order' => 'DESC',
    'posts_per_page' => $post_per_page,
    'ignore_sticky_posts' => 1 // otherwise it breaks pagination
   );

  $wp_query = new WP_Query($args);

    if ( $wp_query->have_posts() ) :

       while ( $wp_query->have_posts() ) { $wp_query->the_post();

   // outputs what you want

  endwhile; 

  endif;

  echo paginate_links(); 

【讨论】:

    【解决方案2】:

    试试下面这个,我合并了两个查询(帖子和页面)。为我工作 - 显示所有带有分页的帖子/页面。

    <?php 
    
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $post_per_page = 5; // How many post per page - setup as you need
    
    $firstQuery = new WP_Query('post_type=post'); // First query - you should change args
    $secondQuery = new WP_Query('post_type=page'); // Second query - you should change args
    
    
    $post_ids = array_merge( $firstQuery, $secondQuery ); // Merge all queries
    
    $query = new WP_Query(
        array(
            'post_type'      => array('post', 'page'), // You should change post types as you need
            'post__in'       => $post_ids, 
            'paged'          => $paged,
            'orderby'        => 'date', 
            'order'          => 'DESC',
            'posts_per_page' => $post_per_page
        )
    );
    
    $totalPosts = $firstQuery->post_count + $secondQuery->post_count; // Count posts
    
    
    if( $query->have_posts() ):
    
        while( $query->have_posts() ): $query->the_post();
    
            echo the_title()."</br>"; // Content
    
        endwhile; 
    
        wp_reset_query();
    
    endif;
    
    
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $totalPosts / $post_per_page, // Total pages
    ) );
    
    ?>
    

    新解决方案 - 编辑

    只需调整第一个和第二个查询

    <?php 
    
    global $wp_query;
    
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $post_per_page = 5; // How many post per page - setup as you need
    
    // First query
    $firstQuery = get_posts(array(
        'posts_per_page' => -1,
        'category_name' => get_query_var( 'category_name' ),
        'post__in'      => $sticky = get_option( 'sticky_posts' ) 
    ));
    
    // Second query 
    $secondQuery = get_posts(array(
        'posts_per_page' => -1,
        'category_name' => get_query_var( 'category_name' ),
        'post__not_in'  => get_option( 'sticky_posts' ),
    ));
    
    
    $mergePosts = array_merge( $firstQuery, $secondQuery ); // Merge all queries
    
    $postIds = array();
    
    foreach( $mergePosts as $post ) {
    
        $postIds[]=$post->ID; // Array with posts ID
    
    }
    
    $uniquePosts = array_unique($postIds); // Create an array with unigue posts
    
    
    // Final query
    $args = array(
    'post_type' => 'any',   
    'post__in' => $uniquePosts,
    'paged' => $paged,
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => $post_per_page,
    );
    
    
    $the_query = new WP_Query($args);
    
    if( $the_query->have_posts() ):
    
        while( $the_query->have_posts() ): $the_query->the_post();
    
            echo the_title()."</br>"; // Content
    
        endwhile; 
    
        wp_reset_query();
    
    endif;
    
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $the_query->max_num_pages
    ) );
    
    ?>  
    

    【讨论】:

    • 它不起作用。即使我纠正了$post_ids = array_merge( $firstQuery-&gt;posts, $secondQuery-&gt;posts ); 中关于array_merge 的错误,这是我的两个查询参数:$args_1 = array('category_name' =&gt; get_query_var( 'category_name' ), 'post__not_in' =&gt; get_option( 'sticky_posts' ), 'category__not_in' =&gt; array(11114), // escludo gli eventi scaduti, 'orderby' =&gt; 'date', 'order' =&gt; 'DESC');
    • $args_2 = array('category_name' =&gt; get_query_var( 'category_name' ),'post__not_in' =&gt; get_option( 'sticky_posts' ),'category__in' =&gt; array(11114), // includo solo gli eventi scaduti,'orderby' =&gt; 'date','order' =&gt; 'DESC',// 'paged' =&gt; $paged);
    • 你把代码放在哪里了?在自定义模板 php 中?这个我测试过并返回所有帖子 - 将与您的查询一起检查。
    • 在 category.php 中,但您的原始代码无法工作,因为 $FirstQuery$SecondQuery 不是数组
    • 是的,你是对的 - 为什么我认为它正在工作,因为我覆盖了 $query 的合并查询并且 post__in 没有被采用;| 无论如何添加了一些其他解决方案 - 请检查。
    【解决方案3】:

    在每个查询中添加以下参数以进行分页

    'paged' => get_query_var('<'page' or something else you defined>')
    

    编辑:嗯,也许这行得通!

    $query1 = new WP_Query($args1); // Has above paginate param
    $query1Ids = get_all_id_in_query1($query1);
    
    $args2 = array(
        'post__not_in' => $query1Ids,
        // Other param
    );
    $query2 = new WP_Query($args2); // Have above paginate param
    $query2Ids = get_all_id_in_query2($query2);
    
    $args3 = array(
        'post__not_in' => array_merge($query1Ids, $query2Ids),
        // Other param, also have paginate param
    );
    $query3 = new WP_Query($args3);
    

    循环每个查询并显示您的帖子。

    【讨论】:

    • 感谢@TungVN,它可以工作,但没有达到预期的效果......假设我设置了每页最多 5 个帖子,假设第一个查询产生 6 个帖子。使用您的代码,这 6 个帖子在页面之间“拆分”;相反,我想首先显示 1.st 查询的所有帖子,而不是 2.nd 查询的所有帖子,然后是 3.d 查询的所有帖子。因此,如果我有 6 个由第一个查询产生的帖子,我将在第一页显示 5/6 个帖子,在第二页显示第六个帖子,然后是其他查询产生的帖子......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多