简体   繁体   中英

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.

<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

 $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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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