Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a site that uses a custom query to search for posts by country.

The search results come back fine, but the pagination at the bottom reflects the total number of posts in the site, instead of the total number of posts in the search results.

The plug in that manages this uses global $wp_query to determine the number of posts, instead of the custom query. By default, it works like this:

function wp_page_numbers($start = "", $end = "")
{
    global $wp_query;
    global $max_page;
    global $paged;
    if ( !$max_page ) { $max_page = $wp_query->max_num_pages; }
    if ( !$paged ) { $paged = 1; }

    $settings = get_option('wp_page_numbers_array');
    $page_of_page = $settings["page_of_page"];
    $page_of_page_text = $settings["page_of_page_text"];
    $page_of_of = $settings["page_of_of"];

    $next_prev_text = $settings["next_prev_text"];
    $show_start_end_numbers = $settings["show_start_end_numbers"];
    $show_page_numbers = $settings["show_page_numbers"];

    $limit_pages = $settings["limit_pages"];
    $nextpage = $settings["nextpage"];
    $prevpage = $settings["prevpage"];
    $startspace = $settings["startspace"];
    $endspace = $settings["endspace"];

    if( $nextpage == "" ) { $nextpage = ">"; }
    if( $prevpage == "" ) { $prevpage = "<"; }
    if( $startspace == "" ) { $startspace = "..."; }
    if( $endspace == "" ) { $endspace = "..."; }

    if($limit_pages == "") { $limit_pages = "10"; }
    elseif ( $limit_pages == "0" ) { $limit_pages = $max_page; }

I tried modifying it to this:

if ($gallery_query){ $page_query=$gallery_query; }else {$page_query=$wp_query;}
    global $max_page;
    global $paged;
    if ( !$max_page ) { $max_page = $page_query->max_num_pages; }
    if ( !$paged ) { $paged = 1; }

But that returns no pages.

Ideally, I should replace the $wp_query value with the $gallery_query value, but I don't know how. The obvious $wp_query = $gallery_query doesn't work.

Any suggestions?

Thanks!

ty

share|improve this question
    
Search for questions containing the posts_clauses filter. There're some examples that will solve your needs (modifying the original query). –  kaiser Jul 3 '12 at 16:08

1 Answer 1

up vote 1 down vote accepted

Firstly, I'd seriously consider opting for another plugin (it hasn't been maintained in over 2 years), or making use of WP's built-in paginate_links.

But, in the quest to provide a solution, give this a go:

function wp_page_numbers_custom( $wp_query, $start = '', $end = '' )
{
    $_backup = array(
        'wp_query' => @ $GLOBALS['wp_query'],
        'max_page' => @ $GLOBALS['max_page'],
           'paged' => @ $GLOBALS['paged'],  
    );

    $GLOBALS['wp_query'] = $wp_query;
    $GLOBALS['max_page'] = $wp_query->max_num_pages;
       $GLOBALS['paged'] = max( $wp_query->get( 'paged' ), 1 );

    wp_page_numbers( $start, $end );

    foreach ( $_backup as $var => $val )
        $GLOBALS[ $var ] = $val;
}

// Example usage: wp_page_numbers_custom( $gallery_query );
share|improve this answer
    
That didn't do it. In a query that only returned four posts, I had still had pagination for 27 pages worth, which is all of the posts on the site. Also, I agree with you about the plug-in, but that's not my call, and to be fair, it works. –  Ty Morton Jul 3 '12 at 19:44
    
Hold the phone! I didn't implement it correctly the first try. It works like a charm! You are awesome! –  Ty Morton Jul 3 '12 at 19:55
    
Got a question: While the pagination works, the links to subsequent pages don't seem to. For example: morepositive.com/category/gallery/?region=US calls 6 pages, which is correct, but clicking on any of the subsequent numbers just takes me back to the same page. Ideas? –  Ty Morton Jul 5 '12 at 19:40
    
Are you manually querying the items, or relying on the global query? –  TheDeadMedic Jul 6 '12 at 8:37
    
It's a manual query. I pass the region variable via the URL, use it to search a separate table that returns an array of countries in that region, and then use that array to build the WP query. –  Ty Morton Jul 6 '12 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.