如何使用分页将结果添加到 wp-query



我正在尝试使用自定义参数将带有分页的结果添加到主查询结果中。但问题是我在每个分页页面中都得到了第二个循环结果。目标是将它们添加到列表的末尾(最后的分页页面(。例如:- 第一个循环获得 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-->

使用它获取分页(每页 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();

现在我从两个查询中得到结果,如下所示:

 <?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 个页面的第二个查询的结果,依此类推。

最新更新