0

First of all thank you everyone for all the knowledge base i got reading trough stackexchange. For the first time i couldn't find an answer to my problem.

The matter is that with this custom query (which has the purpose to search trough the posts with multiple taxonomy fields):

global $wp_query;
$tax_query = array();
$parametri = array(
                        'ih_tipo_struttura' =>  array('tipo_struttura','IN'),
                        'ih_localita'       =>  array('localita', 'IN'),
                        'ih_stelle'         =>  array('stelle', 'IN'),
                        'ih_categorie'      =>  array('categorie', 'AND'),
                        'ih_servizi'        =>  array('servizi', 'AND')
                    );
// Faccio il post dei parametri e li inserisco nella query
foreach ($parametri as $post => $taxonomy) {
    if ( isset($_GET[$post]) && !empty($_GET[$post]) ){ 
        $tax_query[] = array(
                    'taxonomy'  => $taxonomy[0],
                    'field'     => 'slug',
                    'terms'     => $_GET[$post],
                    'operator'  => $taxonomy[1]
                    ); 
    }
}
// Parametri base della query
$args = array( 
    'post_type' => 'post',
    'tax_query' => $tax_query
);
// Parametri paginazione
if ( $pagination ) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args['posts_per_page'] = 2;
    $args['paged'] = $paged;
} else {
    $args[] = array( 'posts_per_page' => 20);   // Se imposto la paginazione a false ritorno i primo 20 risultati
}
// Eseguo la query
$wp_query = new WP_Query( $args );
return $wp_query;

everything works fine until i try to do some pagination.

When i pass trough the url some $_GET parameters like:

http://mywebsite/ricerca/?ih_localita%5B%5D=riccione&ih_localita%5B%5D=rimini&ricerca_hotel=true

or

http://mywebsite/ricerca/page/2/?ih_localita=riccione&ih_localita%5B%5D=rimini&ricerca_hotel=true

there's no problem but if try:

http://mywebsite/ricerca/page/2/?ih_localita%5B0%5D=riccione&ih_localita%5B1%5D=rimini&ricerca_hotel=true

wordpress cut off all the search parameters returning the url just like:

http://mywebsite/ricerca/page/2/?ricerca_hotel=true

I'm getting crazy with this so i would really appreciate any help.

Thx

1

2 Answers 2

2

Don't stomp on the $wp_query global. Use your own variable to hold your query instead:

$custom_query = new WP_Query( $args );

Then, you can use a little trick to fix pagination. Do this before the loop:

global $wp_query;
// Store it for safekeeping
$temp_wp_query = $wp_query;
// Now blank it out
$wp_query = null;
// Now populate it with your custom query
$wp_query = $custom_query;

Then, after the loop closes, restore the original query object:

$wp_query = $temp_wp_query;

Now, your pagination should work properly.

7
  • Thank you for the fast reply but with your solution i'm still having the problem. The matter is not really about the query itself (that is actually working) but more about passing array trough the url ( ih_localita%5B0%5D=riccione ) on paginated url. Any others idea !? Commented May 31, 2012 at 16:39
  • What are those URL query variables, where do they come from, and what are you doing with them? Commented May 31, 2012 at 16:44
  • They are the result of a checkbox array <input '.$checked.' type="checkbox" name="ih_struttura[]" value="'.$term->slug.'">' . $term->name . '</input> Commented May 31, 2012 at 16:47
  • However... i'm thinking about serializing them with some javascript cause i cant find any other solution... something like: mywebsite/ricerca/?ih_localita=term1,term2,term3 actually works using an explode(',', $_GET[$post]); Commented May 31, 2012 at 16:49
  • Might add_query_arg() and get_query_var() be helpful here? Commented May 31, 2012 at 18:02
0

At a second glance i figure out that the problem is probably with url rewrite on pagination which doesn't work well with this url format:

page/2/?ih_localita%5B0%5D=riccione&ih_localita%5B1%5D=rimini

cutting off all the multiple checkbox parameter and rewriting it as:

page/2/

Now my solution will probably be to use another format to pass checkbox multiple value so i will do something like

page/2/?ih_localita=rimini,riccione

that actually works fine but it would be nice to figure out where the problem is...

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.