I made custom post type with one taxonomy. Everything was good except pagination on taxonomy. Firstly, without any changes, I was able to switch pages on main custom post type archive:
website.com/custom_post_type_name/page/x
But when I wanted to jump to taxonomy and switch pages I got 404 error.
website.com/custom_post_type_name/taxonomy_slug/page/x
I tried doing regex from answer from StackExchange but it won't work at all.
add_filter( 'rewrite_rules_array', 'my_insert_rewrite_rules' );
function my_insert_rewrite_rules( $rules ) {
$newrules = array();
$newrules['promocje(/[a-z]+)(/page/([0-9]+))?'] =
'index.php?post_type=promocje'
. '&shop=$matches[1]&paged=$matches[3]';
return $newrules + $rules;
}
promocje
is the name of the post type.shop
is the name of the taxonomy.
After that, I was able to switch pages on taxonomy pages, but I had 404 error when paginating the custom post type.
Code of custom post type and taxonomy:
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Promocje filtrowane przez sklep',
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'page-attributes' ),
'taxonomies' => array( 'shop'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-audio',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'query_var' => true,
'can_export' => true,
'capability_type' => 'post',
'register_meta_box_cb' => 'add_promo_metaboxes',
'rewrite' => array( 'slug' => 'promocje/%shop%', 'with_front' => false ),
'has_archive' => 'promocje',
);
function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'promocje' ){
$terms = wp_get_object_terms( $post->ID, 'shop' );
if( $terms ){
return str_replace( '%shop%' , $terms[0]->slug , $post_link);
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
And taxonomy:
register_taxonomy(
'shop',
'promocje',
array(
'hierarchical' => true,
'label' => 'Sklep',
'query_var' => true,
'rewrite' => array(
'slug' => 'promocje',
'with_front' => false
)
)
);
Any help would be appreciated. I've been trying to solve this for more than a week.
global $wp; $current_url = home_url(add_query_arg(array(),$wp->request)); $arrVals = explode("/",$current_url); $found = 0; foreach ($arrVals as $index => $value) { if($value == 'page') $found = $index; } wp_reset_postdata(); global $wp_query; $args = array_merge( $wp_query->query_vars, array( 'post_type' => 'promocje', 'paged' => $arrVals[$place]) ); $pageposts = new WP_Query($args);
Now I can get every page using hardcode, I also did a code for getting page number from url. How can i put this in pre_get_posts or parse_query?