2

I would like to output related posts on a custom post-type single page. Everything seems to be set up correctly. When I hover over my pagination links I see http://example.com/post-type-slug/post-slug/page/2/. When I click I go to that page, but then get redirected back to the first page: http://example.com/post-type-slug/post-slug/

This is how I've set up my post type:

register_post_type( 'posttype',
    array(
        'rewrite' => array(
            'slug' => 'post-type-slug',
            'with_front' => false,
        ),
        'public' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'has_archive' => false,
        'menu_position'=>5,
    )
);

This is how I am doing my WP_query on the single page

    $queryArgs = array( 
        'posts_per_page' => 3,
    );
    $queryArgs['paged'] = get_query_var( 'page' ) ? get_query_var('page') : 1; 
    $customQuery = new WP_Query( $queryArgs );

For my pagination I am using the method described here: https://wordpress.stackexchange.com/a/172818/10595

Why do I get redirected to the index? What can I do to prevent this.

I checked out some things: When I disable pretty permalinks the pagination links say http://example.com/? posttype=post-slug&paged=2. When I manually change that to http://example.com/? posttype=post-slug&page=2 everything works as I want it to. How can I alter my pagination to do this for me?

0

I found the answer. I had searched high and low yesterday, but today I finally found it: https://wordpress.stackexchange.com/a/143837/10595

add_action('template_redirect', function() {
  if ( is_singular('posttype') ) {
    global $wp_query;
    $page = (int) $wp_query->get('page');
    if ( $page > 1 ) {
      // convert 'page' to 'paged'
      $query->set( 'page', 1 );
      $query->set( 'paged', $page );
    }
    // prevent redirect
    remove_action( 'template_redirect', 'redirect_canonical' );
  }
}, 0 ); // on priority 0 to be able removing 'redirect_canonical' added with priority 10

This, copied to my functions.php, makes WordPress recognize the paged parameter on single posttype entries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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