3

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:

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.

8
  • Would be extra nice to see the query arguments of this advanced search feature.
    – soulseekah
    Mar 10, 2012 at 16:58
  • Please post some code on how you are querying the database, and displaying pagination. Mar 10, 2012 at 17:43
  • Added the query code...
    – jnthnclrk
    Mar 10, 2012 at 18:00
  • What is $pladvsearchcatids? Mar 10, 2012 at 18:07
  • An array of categories.
    – jnthnclrk
    Mar 10, 2012 at 18:18

2 Answers 2

2
+50

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
        )
    )
);
8
  • This didn't work for me.
    – jnthnclrk
    Mar 18, 2012 at 15:27
  • Have you tried making the query simpler? Try taking out some of the variables or just set some variable directly into the query to get some further information about where the query breaks. Mar 18, 2012 at 16:31
  • My point is, there might be multiple issues causing the pagination to break. I'm pretty sure you need to include pagination information in a custom $wp_query Mar 18, 2012 at 16:33
  • No I haven't tried that. It was working fine up until 3.3.1. Sounds like I should start over and build this feature from scratch.
    – jnthnclrk
    Mar 18, 2012 at 16:47
  • That might actually be simpler, but if you find the point that is causing the pagination to fail, please let us know, i need to build a feature like this myself pretty soon. Mar 19, 2012 at 19:07
-1

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.

9
  • Sorry, that didn't work. In fact, it seem to really screw up my results. The first link I posted in the question now only returns 3 results. :)
    – jnthnclrk
    Mar 18, 2012 at 14:52
  • No reason for downvoting before a result is here. What's your settings for posts per page in the admin-settings-page?
    – kaiser
    Mar 18, 2012 at 14:55
  • Posts per page is set to 14.
    – jnthnclrk
    Mar 18, 2012 at 15:01
  • Are you sure, that $plbudget is an integer and not a string? If that's the case, it can't work.
    – kaiser
    Mar 18, 2012 at 15:02
  • Sorry for down-vote, perhaps unnecessary. However, your code did break my ordering.
    – jnthnclrk
    Mar 18, 2012 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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