0

I have multiple custom query vars to filter products with the pre_get_posts hook and the add_query_arg function.

All works fine except the pagination, when i navigate to the page number 5 (for example) without filtering and then i select one filter, if the products for that filter doesn't have 5 pages it will show nothing.

What most sites do is to return to first page when selecting a filter, but i don't know exactly how to do it.

I have this for a start, but of course it doesn't worked. I'll be appreciated if someone helps me.

if ( in_array(get_query_var('brand'), $wp_query->query_vars) ) {
   get_query_var('paged') = 1;
}

How i'm filtering:

function custom_query_vars_filter( $custom_vars ) {
  $custom_vars[] = 'marca';
    return $custom_vars;
  }
add_filter( 'query_vars', 'custom_query_vars_filter' );

function customize_main_query( $query ) {
if ( !is_admin() && $query->is_main_query() ) {

if ( is_product_category() ) {
  $query->set('posts_per_page', 3);    

  $marca= get_query_var('marca');

  $tax_query = array('relation' => 'AND');

  if ( $marca ) {
    $tax_query[] =  array(
      'taxonomy' => 'brands',
      'field' => 'slug',
      'terms' => $marca
    );
  }

  $query->set( 'tax_query', $tax_query );
} 
}
}
add_action( 'pre_get_posts', 'customize_main_query' );

Category archive page

$marca_arg = isset($marca) ? array($marca) : [];

foreach ($marcas_terms as $marca_term) {
   <div class="filtro-item">
      <a href="<?php echo (in_array($marca_term->slug, $marca_arg)) ? 
      esc_url(remove_query_arg('marca')) : esc_url(add_query_arg('marca', 
      "$marca_term->slug")); ?>"><span class="check- 
      filtro"></span><?php echo $marca_term->name; ?></a>
   </div>
}
5
  • How are you doing the actual filtering. How do the filter links work? Mar 23, 2018 at 0:02
  • With the add_query_arg function and calling the custom var in a tax_query Mar 23, 2018 at 0:19
  • Can you add the code to the question. Mar 23, 2018 at 0:40
  • Added, with only one filter to not be too long Mar 23, 2018 at 0:58
  • Where does is_product_category come from? You should be using the provided $query object for those checks, stuff like that always references the main query. Also note that get_query_var('paged') = 1; makes no sense, it's the same as 1 = get_value(), both are meant to appear on the right of the =
    – Tom J Nowell
    Mar 23, 2018 at 1:01

1 Answer 1

1

The problem is the pagination parameter in the URL:

<a href="<?php echo (in_array($marca_term->slug, $marca_arg)) ? 
  esc_url(remove_query_arg('marca')) : esc_url(add_query_arg('marca', 
  "$marca_term->slug")); ?>">

Here we see that the marca term is being added and removed, but the pagination parameter isn't, but that's a part of the URL not a parameter

Instead, why not use the taxonomy archives and get_term_link? Then redirect if the marca parameter is present?

  • You could eliminate the pre_get_posts changes
  • the query var would no longer be necessary
  • the pagination issue would disappear
  • You can use taxonomy-brands.php in your theme
  • You already have archives for this at example.com/brands/*term_name* just waiting for a custom template

They're already on your site, sitting there unused. Why rebuild something that's already there? If you're not keen on the URL you can change it in your taxonomy registration code via the rewrite option

3
  • As i have multiple filters, this will reset the pagination, but also any other filters selected. Mar 23, 2018 at 1:52
  • Then use add_query_arg to modify the term archive link. Eitherway it sounds like an XY problem, where your question is not your problem but a problem with a solution to your problem
    – Tom J Nowell
    Mar 23, 2018 at 14:31
  • Was much simpler than i thought, solve it with this Get current URL (permalink) without /page/{pagenum}/ Mar 23, 2018 at 14:48

Your Answer

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

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