• Hi forum,
    after spending almost a day on this problem I hope that you can help me.
    I have a custom post type “employee”. In a wp_query I want to display all entries and want to have them paginated as well. That all works – on a regular page it does at least.

    However if I insert the same code on my “single-institution.php” (institution is a custom post type as well that ) the pagination does not work anymore. It shows up, it links to the “correct” page (page/2/) but the link always leads back to the first page. It seems the following pages are not generated at all.

    So is this possible at all? Can I have a working pagination for a custom post type on a “single-custom post type page”?

    This is what the query code looks like:

    $args=array(
               'post_type' => "employee",
               'post_status' => 'publish',
                'caller_get_posts' => 1,
                'orderby'           => 'menu_order',
               'order' => 'ASC',
               'posts_per_page' => 3,
               'paged' => $paged
                );
    
    $temp = $wp_query;
    $wp_query = null; 
    
    $wp_query = new WP_Query($args);
    
    // then I loop through the query
    
    previous_posts_link('« Newer') ;
    next_posts_link('Older »');
    
     wp_pagenavi(); 
    
    //wp_reset_query();  // Restore global post data stomped by the_post().    
    
    // Reset the  query
    $wp_query = null;
    $wp_query = $temp;

    The argument array of the “institution” post type lools like this – in case this could be relevant:

    $args = array(
             'label' => __('Institution'),
             'labels' => $labels,
             'public' => true,
             'publicly_queryable' => true,
             'show_ui' => true,
             '_builtin' => false,
             'show_in_menu' => true,
             'query_var' => true,
             'rewrite' => array("slug" => "in"),
             'capability_type' => 'post',
             'has_archive' => true,
             'hierarchical' => true,
             'menu_position' => 20,
             'supports' => $supports
          );

    All help is appreciated!

    Robin

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    What’s happening is the post_link navigation only works for the main query. You can make a new query to get other data, but when you request page 2 using the post_links, it is applied to the main query, not your query, so your query is stuck on page 1.

    To fix this, some people have tried to fool the main query into paginating for the new custom query but it doesn’t seem to always work well. If you don’t need the main query, modify it for your needs with the ‘pre_get_posts’ action instead of making a new query, then the post_link navigation will work flawlessly.

    If you do need the main query, you only recourse is to manage your new query navigation yourself instead of using the post_link template tags.

    Thread Starter robinburrer

    (@robinburrer)

    That explains it! – Thanks for your reply. Since I only have a limited number of possible entries I decided to just get all entries and do the pagination on the client…..

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Nested custom post type pagination’ is closed to new replies.