0

I have a site that uses a custom query to filter posts:

<?php //Search Region table to retrieve array of country names
$regionresult = mysql_query("SELECT * FROM gallery_regions WHERE region='$region'") or die(mysql_error());
$num_rows = mysql_num_rows($regionresult);

// Process search reults and format array contents for WP Query
$country_search_array = array();
    while($row = mysql_fetch_array($regionresult)){
    $country_search_array[] = $row['country']; 
        }

//Build WP query
$args = array(
     'posts_per_page' => '48',
     'cat' => '4',
     'meta_query' => array(
         array(
            'key' => 'Country',
            'value' => $country_search_array,
            'compare' => 'IN'
        )
    )
);
$gallery_query = new WP_Query( $args );?>

supplemented by a custom function that paginates the results, using the WP Page Numbers plugin (http://www.jenst.se/2008/03/29/wp-page-numbers):

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;
}

//usage: wp_page_numbers_custom( $gallery_query );

While the number of pages returned is accurate, the navigation isn't working. If you click on page 2 of X, you still wind up on page 1, despite the change in the URL.

The site in question can be found here.

Any help is appreciated.

Thanks,

ty

2

You need to set paged or an offset in your query, otherwise you're always fetching the same 48 posts.

$page = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
 'paged' => $page,
 'posts_per_page' => '48',
 'cat' => '4',
 'meta_query' => array(
     array(
        'key' => 'Country',
        'value' => $country_search_array,
        'compare' => 'IN'
     )
  )
);
$gallery_query = new WP_Query( $args );?>
| improve this answer | |
  • That's still not working. Am I formatting the query incorrectly? – Ty Morton Jul 9 '12 at 4:21
  • I figured it out: $page = (get_query_var('page')) ? get_query_var('page') : 1; needs to be $page = (get_query_var('paged')) ? get_query_var('paged') : 1;. Thanks so much, Milo! – Ty Morton Jul 12 '12 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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