WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

share|improve this answer
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 Dec 17 '13 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 Dec 17 '13 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 Dec 17 '13 at 14:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.