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)

   comments here removed 

/* 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; ?>

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.

  • The 1 seems to have dissapeared in the copy-paste, you are right about that. The next_posts_link/previous_posts_link funtions get in a string; &larr; and &rarr; are special characters for a left and a right arrow. I know that works. The __() function, as far as I know, marks text for translation, similarly to e.g. _e(). You would then later use a tool to translate all the marked strings.
    – ptf
    Commented Dec 17, 2013 at 13:59
  • I don't know if this __() function is accepted by WordPress. Does it work for you if you only use one string like next_posts_link('older &rarr;', $posts_per_page); or does this also not work?
    – luke
    Commented Dec 17, 2013 at 14:04
  • __() is refered to here -> codex.wordpress.org/… __() basically returns a string, and before I made it an archive page, this worked. The problem I have is probably related to me using this as an archive now, rather than a normal page.
    – ptf
    Commented Dec 17, 2013 at 14:09

You register your post type with the name of reference in this case you can use the built in template hierarchy to get the correct loop. Create a file in the root of your theme folder with the name archive-reference.php.

In this file you don't have to query the references because it will be automatic by WordPress. The only thing that you have to write in this file is a simple logic:

<?php if ( ! have_posts() ) : ?>
  <div class="alert alert-warning">
    <?php _e( 'Sorry, no results were found.', 'kasparabi' ); ?>
<?php endif; ?>

<?php while ( have_posts() ) : the_post(); ?>
  <article <?php post_class(); ?>>
    <h3><?php the_title(); ?></h3>

    <div class="entry-summary"><?php the_excerpt(); ?></div>

    <a href="<?php the_permalink(); ?>"><?php _e('Read More', 'kasparabi'); ?></a>
<?php endwhile; ?>

<?php if ($wp_query->max_num_pages > 1) : ?>
  <nav class="post-nav">
    <ul class="pager">
      <li class="previous"><?php next_posts_link(__('Older', 'kasparabi')); ?></li>
      <li class="next"><?php previous_posts_link(__('Newer', 'kasparabi')); ?></li>
<?php endif; ?>

This will list your references with their title and excerpt.

Don't forget to allow rewrite in your post type declaration:

  $rewrite = array(
    'slug'                  => 'references',
    'with_front'            => false,
    'pages'                 => true,
    'feeds'                 => true,


  'rewrite' => $rewrite,

If you want to query your references on a page, you always have to build a custom query. This answer could help you how to do it: https://stackoverflow.com/a/20905538/2043492

Hope this helps!


/*this one your query*/
global $wp_query;
$wp_query = new WP_Query(array(
    'post_type' => 'reference', 
    'posts_per_page' => 9,
    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1 

/*pagination code here with update max pages*/            

$GLOBALS['wp_query']->max_num_pages = $wp_query->max_num_pages;
    'mid_size' => 1,
    'prev_text' => __('Previous page', 'patelextensions'),
    'next_text' => __('Next page', 'patelextensions'),
    'before_page_number' => '<span class="meta-nav screen-reader-text">' . __('Page', 'patelextensions') . ' </span>',

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.