The Setup
I have 2 taxonomies and 1 post type:
- Post Type:
products
- Hierarchical Taxonomy:
product_cat
- Nonhierarchical Taxonomy:
product_type
The product-category
has a defined endpoint:
add_endpoint( 'types', EP_PRODUCT_CAT );
Now I'm trying to create a fairly lengthy URL that combines both the taxonomies:
https://www.example.com/products/parent-product-cat/child-product-cat/types/top-product-type/
Which works, the types
query var is set, the product_cat
is set to what it needs to be too - rainbows and bubblegum.
The Issue
Pagination. I'm using paginate_links()
which drops my types
query var entirely and assigns it's value to product_cat
query var which results in a 404. The trail begins with add_args
parameter of paginate_links()
. I can pass both product_cat
and types
and pagination works:
global $wp_query;
$term_cat = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$term_type = get_term_by( 'slug', get_query_var( 'types' ), 'product_type' );
echo paginate_links( array(
'base' => '%_%',
'format' => '?paged=%#%',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $wp_query->max_num_pages,
'end_size' => 0,
'mid_size' => 2,
'prev_next' => false,
'add_args' => array(
'product_cat' => $term_cat->slug,
'types' => $term_type->slug,
),
) );
So the query_vars are there now but the permalinks are now ugly since paginate_links()
is just appending them as $_GET
parameters:
https://www.example.com/products/parent-product-cat/child-product-cat/types/top-product-type/page/2/?product_cat=abc&types=123
Since the permalink structure is still intact during pagination I'm unsure if rewrite rules will be helpful at this point - but I've given it a try and it doesn't appear to do much at all:
/**
* Endpoints and Rewrites
*
* @return void
*/
function theme_endpoints_rewrites() {
add_rewrite_rule( '^products/([^/]+)/types/([^/]+)/page/([0-9]+)/?', 'index.php?post_type=products&product_cat=$matches[1]&types=$matches[3]&paged=$matches[4]', 'top' );
add_rewrite_endpoint( 'types', EP_PRODUCT_CAT );
flush_rewrite_rules(true);
}
add_action( 'init', 'theme_endpoints_rewrites' );
I've also tried to modify the pagination links URLs to be home_url()
and attempt to rewrite it given the passed arguments but also to no avail. If I leave the permalink structure as Default ( plain
) the pages load as expected. It seems to happen whenever WordPress rewrites the paged
query var.
The below almost works. It shows that product_cat
has the correct / expected value but it concatenates both types
and paged
query vars into 1 value so that my query var holds [types] => cotton/page/2
and drops the paged query var.
function test_rewrites( $wp_rewrite ) {
$wp_rewrite->rules['products/(.+?)/types/(.+?)/page/?([0-9]{1,})/?$'] = 'index.php?product_cat=$matches[1]&types=$matches[2]&paged=$matches[3]';
unset( $wp_rewrite->rules['products/(.+?)/page/?([0-9]{1,})/?$'] );
unset( $wp_rewrite->extra_rules_top['products/(.+?)/page/?([0-9]{1,})/?$'] );
}
add_action( 'generate_rewrite_rules', 'test_rewrites' );
Does anyone know how I can add pagination to the endpoint while keeping my desired URLs?