如何使用分页将结果添加到wp查询?

时间:2019-07-09 16:46:24

标签: php wordpress

我正在尝试将带有分页的结果添加到具有自定义参数的主查询结果中。但是问题是我在每个分页中都得到了第二个循环结果。目的是将它们添加到列表的末尾(最后的分页页面)。 例如: -第一个循环获得99个结果和10个分页 -第二个循环获得21个结果,并以100到120的顺序列出,从10页分页开始到12。

<div class="property-listing <?php echo esc_attr($listing_view_class); ?>">
                <div class="row">

                    <?php


                    global $wp_query;




                    $sort_args = array(
                        'posts_per_page' => $number_of_prop,
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'property_status',
                                'field'    => 'id',
                                'terms'    => '228',
                                'paged' => $paged,
                                'operator' => 'NOT IN'
                            ),
                        ),
                        'order' => 'DESC',
                        'post_status' => 'publish'
                    );

                    $sort_args = houzez_prop_sort($sort_args);


                    $args = array_merge( $wp_query->query_vars, $sort_args );


                    query_posts( $args );

                    if ( have_posts() ) :
                        while ( have_posts() ) : the_post();

                            if($listing_view == 'listing-style-3') {
                                get_template_part('template-parts/property-for-listing-v3');

                            } else if($listing_view == 'listing-style-2' || $listing_view == 'listing-style-2-grid-view' || $listing_view == 'listing-style-2-grid-view-3-col') {
                                get_template_part('template-parts/property-for-listing', 'v2');

                            } else {
                                get_template_part('template-parts/property-for-listing');
                            }

                        endwhile;


                        wp_reset_postdata();
                    else:
                        ?>
                        <h4><?php esc_html_e('Sorry No Result Found', 'houzez') ?></h4>
                        <?php
                    endif;
                    ?>

                </div>
            </div>





        <hr>

        <!--start Pagination-->
        <?php houzez_pagination( $wp_query->max_num_pages, $range = 2 ); ?>
        <!--start Pagination-->

2 个答案:

答案 0 :(得分:0)

使用它来获取分页(每页50个) 这将100%起作用, 根据结果​​更改参数

 $the_query = new WP_Query( array('posts_per_page'=>50,
    'post_type'=>'Post Type name',
     'paged' => get_query_var('paged') ? get_query_var('paged') : 1) ); 

      <!-- --loop -->
      while ($the_query -> have_posts()) : $the_query -> the_post();  

       the_title();

       endwhile;
       <!-- ---loop -->

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

    wp_reset_postdata();

答案 1 :(得分:0)

现在,我从两个查询中都得到了这样的结果:

 <?php


                global $wp_query, $paged;
                if ( is_front_page()  ) {
                    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
                }


                $sort_args1 = array(
                    'posts_per_page' => $number_of_prop,
                    'paged' => $paged,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'property_status',
                            'field'    => 'id',
                            'terms'    => '228',
                            'operator' => 'NOT IN'
                        ),
                    ),
                    'order' => 'DESC',
                    'post_status' => 'publish'
                );

                $sort_args2 = array(
                    'posts_per_page' => $number_of_prop,
                    'paged' => $paged,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'property_status',
                            'field'    => 'id',
                            'terms'    => '228'
                        ),
                    ),
                    'order' => 'DESC',
                    'post_status' => 'publish'
                );

                $sort_args1 = apply_filters( 'houzez_property_filter', $sort_args1 );
                $sort_args2 = apply_filters( 'houzez_property_filter', $sort_args2 );

                $sort_args1 = houzez_prop_sort($sort_args1);
                $sort_args2 = houzez_prop_sort($sort_args2);


                $args1 = array_merge( $wp_query->query_vars, $sort_args1 );
                $args2 = array_merge( $wp_query->query_vars, $sort_args2 );

                //setup your queries as you already do
                $query1 = new WP_Query($args1);
                $query2 = new WP_Query($args2);






                /*echo "<pre>";
                print_r($args);
                echo "</pre>";
                die();*/


                $wp_query = new WP_Query($wp_query->query_vars);


                $wp_query->posts = array_merge( $query1->posts, $query2->posts );
                $wp_query->post_count = $query1->post_count + $query2->post_count;

                //$wp_query = apply_filters( 'houzez_property_filter', $wp_query->posts );
                //$wp_query = houzez_prop_sort($wp_query);





                if ( $wp_query->have_posts() ) :
                while ( $wp_query->have_posts() ) : $wp_query->the_post();

                        if($listing_view == 'listing-style-3') {
                            get_template_part('template-parts/property-for-listing-v3');

                        } else if($listing_view == 'listing-style-2' || $listing_view == 'listing-style-2-grid-view' || $listing_view == 'listing-style-2-grid-view-3-col') {
                            get_template_part('template-parts/property-for-listing', 'v2');

                        } else {
                            get_template_part('template-parts/property-for-listing');
                        }

                    endwhile;


                    wp_reset_query();
                else:
                    ?>
                    <h4><?php esc_html_e('Sorry No Result Found', 'houzez') ?></h4>
                    <?php
                endif;
                ?>

            </div>
        </div>
        <!--end property items-->





        <hr>

        <!--start Pagination-->
        <?php houzez_pagination( $wp_query->max_num_pages, $range = 2 ); ?>

但是问题是我在第一个查询的第15页结果和第二个查询的15页结果中都得到了这些结果。我的目标是从第一个查询接收所有结果(例如150)-将是10个分页器链接,然后从11页显示第二个查询的结果,依此类推。