0

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

6
  • Are you referring to medias/actualites/page/2 and pages 3, 4, etc.? If so, then you should enable archive for the post type, i.e. 'has_archive' => true. But what does the page at medias/actualites currently show? And by "translate", did you mean you're redirecting medias/actualites to media/press-news?
    – Sally CJ
    Commented Aug 6, 2020 at 14:12
  • Yes, I'm refering to those pages. I wanted to do this by archive-actualite, but my plugin (polylang (not pro)) doesn't accept the translation of the page title. medias/actualites shows all of my actualite with a custom wp_query Commented Aug 6, 2020 at 14:36
  • Lol sorry about the "translate" thing.. So are the medias/actualites and media/press-news static Pages (i.e. posts of the page type)?
    – Sally CJ
    Commented Aug 6, 2020 at 15:02
  • When has_archive is false (i.e. archive is not enabled), then medias/actualites will naturally result in a 404 error, but you said that it displays all the posts in your CPT, hence I asked if you've got a static page at that URL. But even so, the 404 error on pages 2, 3, etc. is normal since WordPress treats the URL (e.g. medias/actualites/page/2) as a single CPT request.
    – Sally CJ
    Commented Aug 6, 2020 at 17:26
  • It display my posts because it's indeed a custom page (as I said, not archive-actualite.php). So how can I "fix" the 404 ? Commented Aug 7, 2020 at 7:52

1 Answer 1

2

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.

2
  • That's great, it's working on the french version ! But how can I do this with my english version ? I've tried to replace medias/actualites with en/press-news/press ... Commented Aug 10, 2020 at 7:37
  • 1
    Help a lot ! For the english version, did this to make it works : add_rewrite_rule( '^en/press-news/news/page/(\d+)/?$', 'index.php?pagename=press-news/news&paged=$matches[1]', 'top' ); Commented Aug 10, 2020 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.