2

I have a problem with pagination.

I created a taxonomy template 'taxonomy-product_cat-sale.php' to display products that are on sales.

Everything is ok, but when I click to the next page (mysite.com/sale/page/2/) I redirect to 404 page.

I followed this answer https://wordpress.stackexchange.com/a/304375

and here is my code

$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;

$ordering = WC()->query->get_catalog_ordering_args();
$ordering_explode = explode(' ', $ordering['orderby']);
$ordering['orderby'] = array_shift($ordering_explode);
$ordering['orderby'] = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];

$products_per_page = apply_filters('loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page());

$product_ids_on_sale = wc_get_product_ids_on_sale();
$args = array(
  'status' => 'publish',
  'include' => $product_ids_on_sale,
  'stock_status' => 'instock',
  'limit' => $products_per_page,
  'page'  => $paged,
  'paginate' => true,
  'orderby' => $ordering['orderby'],
  'order' => $ordering['order'],
);

$query_results = wc_get_products( $args );

wc_set_loop_prop('current_page', $paged);
wc_set_loop_prop('is_paginated', wc_string_to_bool(true));
wc_set_loop_prop('page_template', get_page_template_slug());
wc_set_loop_prop('per_page', $products_per_page);
wc_set_loop_prop('total', $query_results->total);
wc_set_loop_prop('total_pages', $query_results->max_num_pages);

Any ideas?

Emily
  • 154
  • 1
  • 4
  • 19

0 Answers0