[resolved] Custom Post Type Singular Page with Pagination (5 posts)

  1. Patrick Daly
    Posted 5 years ago #

    The Problem

    I'm unable to do any sort of pagination (built in or plugin) with a WP_Query on a singular post type page using custom permalinks.

    Pagination does work with the default permalink structure.

    The Code

    Registering the Post Type

    /* Arguments for the client post type. */
    $client_args = array(
    	'labels' => $client_labels,
    	'capability_type' => 'page',
    	'public' => true,
    	'can_export' => true,
    	'query_var' => true,
    	'rewrite' => array( 'slug' => 'client', 'with_front' => false ),
    	'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', "{$prefix}-post-settings", 'entry-views' ),
    	'taxonomy' => array( 'category' )
    register_post_type( apply_filters( 'mba_client_post_type', 'client' ), apply_filters( 'mba_client_post_type_args', $client_args ) );

    The Loop

    $slug = basename(get_permalink());
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $args = array( 'posts_per_page' => '-1', 'post_type' => 'press', 'clients' => $slug, 'paged' => $paged );
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    <!-- do stuff -->
    <?php endwhile; ?>
    </div><!-- .gallery -->
    <?php previous_posts_link(); ?>
    <?php next_posts_link(); ?>
    <?php $wp_query = null; $wp_query = $temp; ?>
    <?php wp_reset_query(); ?>


    My post_type is client so I'm using client.php to show the singular post. The template then pulls in posts from another post type, press. The press posts should pagination.

    Desired URLs: http://maxborgesagency.com/client/ibuypower/ and http://maxborgesagency.com/client/ibuypower/page/2/

    Currently when you go to http://maxborgesagency.com/client/ibuypower/page/2/ you are redirected back to http://maxborgesagency.com/client/ibuypower/

    So those desired URLs don't work, but these do when the permalink structure is set to default: http://maxborgesagency.com/?client=ibuypower&paged=2

    I'm thinking that WordPress just isn't able to understand the rewritten URL properly.

    Any ideas?

  2. vtxyzzy
    Posted 5 years ago #

    Try setting $paged just before the $args array like this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array( 'posts_per_page' => '-1', 'post_type' => 'press', 'clients' => $slug, 'paged' => $paged );
  3. Justin Tadlock
    WordPress God
    Posted 5 years ago #

    WP redirects this except for with the page post type. Something like this or a variation of it should work:

    add_filter( 'redirect_canonical', 'my_disable_redirect_canonical' );
    function my_disable_redirect_canonical( $redirect_url ) {
    	if ( is_singular( 'client' ) )
    		$redirect_url = false;
    	return $redirect_url;
  4. Patrick Daly
    Posted 5 years ago #

    You make me sick.

    I spend 3 days trying to figure this out and it takes you 3 minutes to write that post and solve all my woes.

  5. Justin Tadlock
    WordPress God
    Posted 5 years ago #

    Scribu actually pointed out redirect_canonical to me a few months ago:

Topic Closed

This topic has been closed to new replies.

About this Topic