Using $paged redirects /page/2 to page 1

I've setup a custom query for my posts (single.php) with pagination, which by the way is working great with the default permalink structure.

domain.com/p=IDpaged=2

if I switch the permalinks to /%postname%/ the page/2/ redirects to the first page.

?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array( 'connected_type'= 'posts_to_posts', 'posts_per_page' = 3, 'paged' = $paged, 'order' = 'ASC', 'connected_items' = get_queried_object() );

$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query-query( $args );
if ( $wp_query-have_posts() ) : while ( $wp_query-have_posts() ) : $wp_query-the_post(); ?

    a href="?php the_permalink(); ?" id="post-?php the_ID(); ?"?php the_title(); ?/a

?php endwhile; endif; $wp_query = null; $wp_query = $temp; wp_reset_query(); ?

What's confusing me is the fact that ir's working with the default permalinks, why isn't in working with the "pretty" links?

Homepage pagination is working fine, just the post page that's messed up somehow. Can anyone point me to the right direction? where should I be looking for errors?

I've deactivated all plugins, deleted htaccess and created a new one, still nothing.

Topic paged wp-query Wordpress

Category Web


It is very old custom post type pagination issue. And has open issue on wordpress core development tracker The last workaround fix that figure in tracker was these one below. It working great for me.

/**
 * Fix pagination issue on a custom post type
 *
 * @link https://core.trac.wordpress.org/ticket/15551
 *
 * @param object $request WP_Query
 *
 * @return object
 */
function child_martfury_fix_request_redirect( $request ) {
    if ( isset( $request->query_vars['post_type'] )
         && 'custom_type' === $request->query_vars['post_type']
         && true === $request->is_singular
         && - 1 == $request->current_post
         && true === $request->is_paged
    ) {
        add_filter( 'redirect_canonical', '__return_false' );
    }

    return $request;
}
add_action( 'parse_query', 'child_martfury_fix_request_redirect' );

If anyone comes across this issue, you can add this to your functions.php code

add_filter('redirect_canonical','pif_disable_redirect_canonical');

function pif_disable_redirect_canonical($redirect_url) {
    if (is_singular()) $redirect_url = false;
return $redirect_url;
}

Source


If this is not the main loop. aka (Blog Page) Wordpress will interprets the /2 as a subpage name and not as a paging query_var.

If you want this to work on a page you need to registers a custom query_var http://codex.wordpress.org/WordPress_Query_Vars and make the structure /page/paging/2 for example.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.