Pagination with custom post type not working

For some reason I can’g get pagination using previous_posts_link and next_posts_link working.

Here is the code I have…

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

<?php // The Query
$the_query = new WP_Query(
array(
    'post_type'=>'article',
    'posts_per_page'=>2,
    'orderby'=>'date',
    'paged'=>$paged
)
);

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();

if($the_query->current_post == 0 && $paged == 1) :
?>
<article class="latest">
    <div class="summary">
        <h4>LATEST ARTICLE:</h4>

        <h2><?=the_title(); ?></h2>
        <div class="entry-meta">
            <?php proagent_posted_on(); ?>
        </div><!-- .entry-meta -->
        <div class="entry-content">
            <?=get_the_content_limit(300, 'More'); ?>

        </div><!-- .entry-content -->

        <div class="entry-topics"><?=get_the_term_list($post->ID , 'topics', 'Topics: ',', '); ?></div>
    </div>
    <?=the_post_thumbnail('feature-post-thumbnail'); ?>
</article>

<?php else: ?>
<article class="previous">
    <?=the_post_thumbnail(); ?>
    <div class="summary">
        <h2><?=the_title(); ?></h2>
        <div class="entry-meta">
            <?php proagent_posted_on(); ?>
        </div><!-- .entry-meta -->
        <div class="entry-content">
            <?=get_the_content_limit(300, 'More'); ?>

        </div><!-- .entry-content -->

        <div class="entry-topics"><?=get_the_term_list($post->ID , 'topics', 'Topics: ',', '); ?></div>
    </div>
</article>
<?php endif; ?>

<?php endwhile; ?>
<nav id="nav-below" class="navigation">
    <div class="alignleft"><?php previous_posts_link('« Newer Articles') ?></div>
    <div class="alignright"><?php next_posts_link('Older Articles »') ?></div>
</nav><!-- #nav-below -->

Also.. just noticed it works when I go to /articles/page/2 there is a link to /articles/ like there should be but from the /articles/ page there is no link to page 2.. WHY?

PLEASE help if you can… this is driving me crazy.

Thanks! mark.

2 s
2

The pagination works only, if the paged attribute in the the global $wp_query variable is set. You could store your query manually in this global or just use query_posts() instead of new WP_Query().

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = query_posts(
        array(
            'post_type'=>'article',
            'posts_per_page'=>2,
            'orderby'=>'date',
            'paged'=>$paged
        )
    );
    // ...
?>

The function query_posts() will unset an existing $wp_query global, runs a new WP_Query() and stores the result again in the global $wp_query variable.

Leave a Comment