WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Patrick Daly
    Member
    Posted 3 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

    <?php
    $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 );
    $wp_query->query($args);
    ?>
    <?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(); ?>

    Template

    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
    Member
    Posted 3 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
    Member
    Posted 3 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
    Member
    Posted 3 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
    Member
    Posted 3 years ago #

    Scribu actually pointed out redirect_canonical to me a few months ago:
    http://www.wptavern.com/forum/bbpress/1710-bbpress-alternative-wordpress-2.html#post16728

Topic Closed

This topic has been closed to new replies.

About this Topic