Pagination on custom query

I’m using the following code with query_posts to put together a custom search:

$args = array(
    'post_type' => 'species',
    'meta_query' => $meta_query,
    'tax_query' => $tax_query
);

$meta_query is put together with a few segments of code like this:

Read More
if (!empty($_POST["s_aquarium_H"])) {
    $val = convert_values('size', $size, $_POST["s_aquarium_H"]);
    $aquarium_H = array(
        'key' => 'aquarium_H',
        'value' => $val,
        'type' => 'numeric',
        'compare' => '<='
    );
    $meta_query[] = $aquarium_H;
}

This works perfectly well and is accessed by visiting /advanced-search/.

However, my pagination does not. When you click Next Entries, the URL changes to /advanced-search/page/2/ but simply re-opens the Advanced Search form.

I guess this is because I’m using $_POST. Is there a way of using pagination with $_POST or do I need to change my code to get_query_var or something?


EDIT

My rewrite rules appear to be correct: /advanced-search/page/2/ displays pagename: advanced-search, paged: 2 in Monkeyman’s Rewrite Analyzer plugin.

Rewrite Rules


FURTHER EDIT

My advanced-search.php page uses the following code to determine whether it should show the search form or the results. I guess this needs re-writing if I’m to use pagination:

<?php if ( isset( $_POST["act"] ) && $_POST["act"] == "s" ) : ?>

Does anyone have any ideas as to how I could go about rewriting this? I guess GET instead of POST needs to be used?


Thanks in advance,

Related posts

Leave a Reply

3 comments

  1. Add this to your query:

    'paged' => get_query_var('paged')
    

    Like so:

    $args = array(
        'post_type' => 'species',
        'meta_query' => $meta_query,
        'tax_query' => $tax_query,
        'paged' => get_query_var('paged')
    );
    

    Should do the trick.. Using Wp-pageNavi? dont forget to add the
    wp-pagenavi tag just after the loop ends

    <div class="navigation"><?php wp_pagenavi(); ?></div>
    

    .
    Hope This Helps 😉
    Cheers, Sagive.

  2. if (!empty($_REQUEST["s_aquarium_H"])) {
       $_GET["s_aquarium_H"] = $_REQUEST["s_aquarium_H"];
        $val = convert_values('size', $size, $_REQUEST["s_aquarium_H"]);
        $aquarium_H = array(
            'key' => 'aquarium_H',
            'value' => $val,
            'type' => 'numeric',
            'compare' => '<='
        );
        $meta_query[] = $aquarium_H;
    }
    
  3. In the end, this was nothing to do with pagination – as such.

    I needed to change my entire form to GET method and $_REQUEST variables rather than POST and $_POST.