I can’t find the way of making my pagination to work on my post type (this is not on an archive-posttype.php). I’ve rewrited my post type, but the second page isn’t working if I do the rewriting (“medias/actualites”), but it works if I have the non-rewrited slug (“actualite”).
I do the rewrite because my client wants to have the “medias/actualites” in the url, but with that, the pagination return a 404 on the second page…
This is my post type
$args = array(
'menu_icon' => 'dashicons-admin-site-alt2',
'label' => __('Actualités', 'domain'),
'description' => __('Actualités', 'domain'),
'labels' => $labels,
'supports' => array('title', 'page-attributes', 'editor', 'thumbnail', 'revisions', 'post-formats', 'excerpt',),
'taxonomies' => array('category', 'post_tag'),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'rewrite' => array(
'slug' => 'medias/actualites',
'with_front' => false,
),
);
Edit : and I’m using a plugin to translate /medias/actualites to /media/press-news
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
So based on the comments, you confirmed that you’re using a custom Page (i.e. post of the page type) as the post type’s archive, which means you either have both the media and actualites Pages (where actualites is a child of the media Page) or that you used a parent Page for the CPT archive.
And normally, going to page 2, 3, etc. of a Page will not going to result in a 404 error; however, your post type’s rewrite slug is medias/actualites, therefore medias/actualites/page/2 (or page/3, page/4, etc.) will result in the 404 error because WordPress treats the URL as a single CPT request.
But fortunately, you can get rid of the 404 error by using a custom rewrite rule which can be added using add_rewrite_rule():
// First, register the CPT.
register_post_type( 'actualite', array( ... your args ... ) );
// Then add the rewrite rule.
// Note: With pagename, you must use the full page path, i.e. <parent slug>/<child slug>
add_rewrite_rule(
'^medias/actualites/page/(d+)/?$',
'index.php?pagename=medias/actualites&paged=$matches[1]',
'top'
);
/* Or if using a parent Page:
add_rewrite_rule(
'^medias/actualites/page/(d+)/?$',
'index.php?pagename=your-cpt-archive&paged=$matches[1]',
'top'
);
*/
PS: Don’t forget to flush the rewrite rules and just use get_query_var( 'paged' ) to get the page number.
And if you already have a custom rewrite rule, you can add it to the question and I’ll help you fix it.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0