Custom Post Type – CPT Archive Pagination – Page not found

I have the following code to register the CPT, the respective taxonomy, and rewrite the rules.

$store_name="store"
add_action('init', function () use ($store_name) {
  register_post_type(
    'my_sample_cpt',
    array(
      'public'      => true,
      'has_archive'  => $store_name,
    )
  );
  register_taxonomy(
    'collection',
    'my_sample_cpt',
    array(
      'label' => __('Collection'),
      'rewrite' => array('slug' => $store_name),
      'hierarchical' => true,
      'show_admin_column' => true,
    )
  );

  add_rewrite_tag('%collection%', '([^&]+)');
  add_rewrite_tag('%pageno%', '(\d+)');

  add_rewrite_rule($store_name . '/([^/]*)/page/(\d+)/?$', 'index.php?post_type=my_sample_cpt&collection=$matches[1]&pageno=$matches[2]', 'top');
  add_rewrite_rule($store_name . '/page/(\d+)/?$', 'index.php?post_type=my_sample_cpt&pageno=$matches[1]', 'top');

  flush_rewrite_rules(false);
}, 100);

The following urls are working fine

  • example.com/store/
  • example.com/store/my-Collection/
  • example.com/store/my-Collection/page/2/

But when I hit the below url, it says page not found

  • example.com/store/page/2/

This is the related rewrite rule I used to get the URL structure.

add_rewrite_rule($store_name . '/page/(\d+)/?$', 'index.php?post_type=my_sample_cpt&pageno=$matches[1]', 'top');

Any advice, some code?

Leave a Comment