简体   繁体   English

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

[英]How to add results to wp-query with pagination?

I'm trying to add results with pagination to the main query results with custom arguments. 我试图将带有分页的结果添加到具有自定义参数的主查询结果中。 But the problem is that I'm getting the second loop result in every pagination page. 但是问题是我在每个分页中都得到了第二个循环结果。 The goal is to add them to the end of list (last pagination pages). 目的是将它们添加到列表的末尾(最后的分页页面)。 For example: - The first loop is getting 99 results and 10 pagination pages - The second loop is getting 21 results and listed as 100 to 120, starting from 10 pagination page to 12. 例如:-第一个循环将获得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-->

Use This to get the pagination (50 per page) This will work 100%, Change the parameters according to the result 使用此获取分页(每页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();

Right now I'm getting the result from both Queries like this: 现在,我从两个查询中都得到了这样的结果:

 <?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 ); ?>

But the problem is that I'm getting those results on every page 15 results from first Query + 15 results from the second. 但是问题是我从第一个查询获得15个结果,而从第二个查询获得15个结果。 And my goal is to receive ALL of the results from first Query (for example 150) - it will be 10 paginator links and then display result from the second Query from 11 page and so on. 我的目标是从第一个查询接收所有结果(例如150)-将是10个分页器链接,然后从11页显示第二个查询的结果,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM