Custom post type archive page pagination

I have set up an archive page for my custom post type reference, where I use a query to get all posts of type reference. This works, but when I try to paginate the result, it doesn’t work. Right now, it doesn’t even print out the pagination links.

EDIT: This was earlier used on a regular page, but I changed it to an archive page. That’s when the pagination broke.

Here is the source code;

Custom post type reference (plugin)

<?php
   /*
   comments here removed 
   */
?>
<?php

/* Custom post types */
function create_references_post_type() {
    $labels = array(
        'name'               => __( 'References', 'kasparabi' ),
        'singular_name'      => __( 'Reference', 'kasparabi' ),
        'add_new'            => __( 'Add New', 'kasparabi' ),
        'add_new_item'       => __( 'Add new reference', 'kasparabi' ),
        'edit_item'          => __( 'Edit reference', 'kasparabi' ),
        'new_item'           => __( 'New reference', 'kasparabi' ),
        'all_items'          => __( 'All references', 'kasparabi' ),
        'view_item'          => __( 'View reference', 'kasparabi' ),
        'search_items'       => __( 'Search references', 'kasparabi' ),
        'not_found'          => __( 'No references found', 'kasparabi' ),
        'not_found_in_trash' => __( 'No references found in the Trash', 'kasparabi' ), 
        'parent_item_colon'  => '',
        'menu_name'          => __('References', 'kasparabi')
    );
    $args = array(
        'labels'        => $labels,
        'description'   => __('All the references', 'kasparabi'),
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt'),
        'has_archive'   => __('references', 'kasparabi')/*,
        'rewrite'       => array('slug' => __('references', 'kasparabi'))*/
    );
    register_post_type( 'reference', $args );   
}
add_action('init', 'create_references_post_type');

function add_post_type_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'reference' ) );
    return $query;
}
add_action( 'pre_get_posts', 'add_post_type_to_query' );

?>

The way I query for the posts:

$wp_query = new WP_Query(array(
                        'post_type' => 'reference', 
                        'posts_per_page' => 9,
                        'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1 
                    ));

How I print out the pagination links:

<div class="col-xs-12">
    <?php previous_posts_link(__('&larr; newer', 'kasparabi')); ?>

    <?php if ($loop->max_num_pages > $paged) :
        next_posts_link(__('older &rarr;', 'kasparabi'), $posts_per_page);
    endif; ?>
</div>

And as a bonus question; is there another way to get the posts than creating a new query every time?

3 Answers
3

Shouldn’t it be like this?

'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1

And next_posts_link/previous_posts_links has these arguments:

 next_posts_link( $label , $max_pages );

$label is not a string in your code but a kind of an array?

I am not so familiar with these “magical PHP functions”, but what do you suppose these “__” to do? As far as I am informed, one should not use these.

Leave a Comment