2

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=ID&paged=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.

4 Answers 4

5

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

2
  • 1
    doing this will have other consequences which may be undesirable, as you're disabling all canonical redirects for all singular posts, use at your own risk.
    – Milo
    Commented Jan 12, 2014 at 19:36
  • @Milo Is there an alternative solution to this? Commented Jan 26, 2021 at 12:24
3

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' );
1

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.

3
  • But, if that's the case, it shouldn't work with the default permalinks as well, am I wrong? if you have any idea I can try, please let me know.
    – user156
    Commented Jan 12, 2014 at 17:12
  • Yes you are :) The "Default" permalink do not need to rewrite its query_vars, so most GET parameters will be parsed and be accessible. That's why it works. Commented Jan 12, 2014 at 17:22
  • Cheers, you made me look on a different direction. I looked for redirection problems with the pagination and I came across this Solution, worked great with my current code. :)
    – user156
    Commented Jan 12, 2014 at 17:39
0

I've tried best-noted workaround and it didn't worked for me - using /page-name/?page=2 still redirected me to /page-name/.

Also noticed using ?p=2 didn't work either.

The only workaround I found is using another parameter name, for example ?pg=2 which doesn't cause redirect and is accessible via $_GET['pg'].

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.