What could be causing my wp_query pagination to break?

I’m using categories and wp_query to create an “advanced search” feature on my website. It was working great until recently I noticed that the pagination was broken. The following 2 links should demonstrate the problem:

On page 1 I get 185 results:

Read More

http://www.barbadospropertylist.com/?cat%5Boffer%5D=24&cat%5Btype%5D=-1&cat%5Blocation%5D=-1&cat%5Bbedrooms%5D=-1&cat%5Bfurnishings%5D=-1&budget=1000000&cat%5Bparish%5D=-1&cat%5Bagent%5D=-1&cat%5Bbathrooms%5D=-1&submit=Advanced+Search

But on subsequent pages the query string breaks:

http://www.barbadospropertylist.com/page/2/?cat%5Boffer%5D=24&cat%5Btype%5D=-1&cat%5Blocation%5D=-1&cat%5Bbedrooms%5D=-1&cat%5Bfurnishings%5D=-1&cat%5Bparish%5D=-1&cat%5Bagent%5D=-1&cat%5Bbathrooms%5D=-1&budget=1000000&submit=Advanced+Search

You should see the second link redirect to:

http://www.barbadospropertylist.com/page/2/?budget=1000000&submit=Advanced%20Search

What with recent upgrades to WordPress I have no idea what might be causing this. Any pointers would be very, very welcome! I’ve been trying to figure this one out all day. 🙂

Here is my query code:

    query_posts(
    array_merge(
        wp_query->query,
        array(
            'category__and' => $pladvsearchcatids,
            'meta_key' => 'price',
            'orderby' => 'meta_value_num',
            'meta_compare' => '<=',
            'meta_value' => $plbudget,
            'order' => 'DESC'
        )
    )
);

Just noticed something really strange. When I download my site and work locally the problem goes away. The query string is not redirected on page 2 and paging through the results works as expected.

Related posts

Leave a Reply

2 comments

  1. This might happen because you need to include the pagination information when you run custom queries with query_posts. Because custom query_post commands ignore any default values of the query_posts command. Here is an example of a query i have used to solve this problem in a simpler case (just to exclude a single category from the query):

       <?php
             if ( is_home() ) {
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                query_posts("cat=-19&paged=$paged");
             }
        ?>
    

    Here is an example of how you can include this in your query. Don’t know if this works for you, try it out and give me feedback. 🙂

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
        query_posts(
        array_merge(
            $wp_query->query,
            array(
                'category__and' => $pladvsearchcatids,
                'meta_key' => 'price',
                'orderby' => 'meta_value_num',
                'meta_compare' => '<=',
                'meta_value' => $plbudget,
                'order' => 'DESC',
                'paged' =>  $paged
            )
        )
    );
    
  2. Please try the following code:

    $result = query_posts(
        wp_parse_args(
            $GLOBALS['wp_query']->query,
            array(
                'category__and' => $pladvsearchcatids,
                'meta_query' => array(
                    array(
                        'key' => 'price',
                        'value' => $plbudget,
                        'compare' => '<='
                    )
                ),
                'orderby' => 'meta_value_num',
                'order' => 'DESC',
                'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1
            )
        )
    );
    // Check the output/query result:
    echo '<pre>'; print_r( $result ); echo '</pre>';
    

    Note, that your orberby value accepts only numeric values. You could try meta_value instead, if your output are (eventually) strings. In this case, the compare statement of <= won’t work.