Support » Plugin: WP-PageNavi » WP-PageNavi with custom query and $paged variable

  • Resolved rbaier4


    I was using Scribu’s instructions, to get WP-PageNavi working with my custom queries and everything was working fine for most of my site, except for my home page. It is a static page with two queries, the default query to display the contents of the page, followed by a custom query.

    There are two separate issues I had to solve to make my particular setup work. First, if you reference the WP docs for WP_Query, there is a small note that says

    Pagination Note: You should set get_query_var( 'page' ); if you want your query to work with pagination. Since WordPress 3.0.2, you do get_query_var( 'page' ) instead of get_query_var( 'paged' ). The pagination parameter ‘paged’ for WP_Query() remains the same.

    I’m using WP_Query on both my homepage and my category pages, so why get_query_var('paged') worked fine for me on my category pages and not my homepage is beyond me.

    So, after solving that problem, the navigation on my homepage was almost correct, except the “next page” link was always pointing to the second page, no matter what I did. I tracked this down to the WP function get_next_posts_link used by WP-PageNavi. That function uses a global $paged variable rather than the query variable of the same name, which was never getting set on my homepage.

    So, the code I’m now using on all my pages with custom queries looks something like this (this can be simplified but I expanded it here for clarity):

    global $paged;
    if( get_query_var( 'paged' ) )
    	$my_page = get_query_var( 'paged' );
    else {
    	if( get_query_var( 'page' ) )
    		$my_page = get_query_var( 'page' );
    		$my_page = 1;
    	set_query_var( 'paged', $my_page );
    	$paged = $my_page;
    // default loop here, if applicable, followed by wp_reset_query();
    $args = array(
    	// other query params here,
    	'paged' => $my_page
    $my_query = new WP_Query( $args );
    // custom loop code
    wp_pagenavi( array( 'query' => $my_query ) );

    This may be a specific case (static homepage with a default query followed by a custom query and using WP-PageNavi), but it took me a fair amount of time to solve and I thought I’d share what worked for me.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘WP-PageNavi with custom query and $paged variable’ is closed to new replies.