user4572969
user4572969

Reputation: 33

Wordpress WP-PageNavi not working with custom query

I have been trying to solve this for a while now but with no success. I have read other similar posts but they are not working for me.

I have created a custom query with displays the correct results and the pagination shows but when i click on the page 2 etc, the url changes accordingly but the same posts remain.

My custom query is:

$sale_properties = new WP_Query(array(
'post_type'         => 'properties',
'meta_key'          => $nvr_initial.'_price',
'meta_value'        => $nvr_price,
'orderby'           => 'meta_value_num',
'order'             => 'DESC',
'paged'             => get_query_var('page'),
'meta_query' => array(
                array('key' => $nvr_initial.'_status',
                    'value' => array('For Sale'),),),));

and my other code is:

<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if (  $sale_properties->max_num_pages > 1 ) : ?>
 <?php if(function_exists('wp_pagenavi')) { ?>
     <?php wp_pagenavi( array( 'query' => $sale_properties ) ); ?>
 <?php }else{ ?>
    <div id="nav-below" class="navigation">
            <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Previous', THE_LANG ) ); ?></div>
            <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">&rarr;</span>', THE_LANG ) ); ?></div>
    </div><!-- #nav-below -->
<?php }?>
<?php endif; wp_reset_query();?>

I've tried page, paged and:

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

Could someone please help me out as it's driving me crazy

Kind regards

S

Upvotes: 1

Views: 1921

Answers (2)

user4572969
user4572969

Reputation: 33

I had my custom wp query in the functions.php file. After moving it to my custom page template it worked

$sale_properties = new WP_Query(array(
'post_type'         => 'properties',
'meta_key'          => $nvr_initial.'_price',
'meta_value'        => $nvr_price,
'orderby'           => 'meta_value_num',
'order'             => 'DESC',
'paged'             => get_query_var('paged'),
'meta_query' => array(
                array('key' => $nvr_initial.'_status',
                    'value' => array('For Sale'),),),));

Upvotes: 0

Pankaj Verma
Pankaj Verma

Reputation: 415

Replace page with paged in below code:

    $sale_properties = new WP_Query(array(
'post_type'         => 'properties',
'meta_key'          => $nvr_initial.'_price',
'meta_value'        => $nvr_price,
'orderby'           => 'meta_value_num',
'order'             => 'DESC',
'paged'             => get_query_var('page'),
'meta_query' => array(
                array('key' => $nvr_initial.'_status',
                    'value' => array('For Sale'),),),));

to

$sale_properties = new WP_Query(array(
'post_type'         => 'properties',
'meta_key'          => $nvr_initial.'_price',
'meta_value'        => $nvr_price,
'orderby'           => 'meta_value_num',
'order'             => 'DESC',
'paged'             => get_query_var('paged'),
'meta_query' => array(
                array('key' => $nvr_initial.'_status',
                    'value' => array('For Sale'),),),));

or put

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

above the query and pass $paged variable in query at the place of

get_query_var('page'),

Hopes it will help.

EDIT:

Wordpress standard way:

place on top:

$big = 999999999; 
    $current_page = get_query_var( 'paged', 1 );
    $args = array(
                    //your query arguments
                    'paged' => $current_page
            );
    $my_query = new WP_Query($args);

Use loop like below:

while ( $my_query->have_posts() ) : $my_query->the_post();
// your code
endwhile;

then

echo paginate_links( array(
                            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                            'format' => '?paged=%#%',
                            'current' => max( 1, get_query_var('paged') ),
                            'total' => $my_query->max_num_pages
                            ) );

place above where you want to print paging.

Upvotes: 1

Related Questions