WordPress.org

Ready to get started?Download WordPress

Forums

How get pagination to work when querying for custom type posts on a category (4 posts)

  1. xrayeye
    Member
    Posted 1 year ago #

    Hi there,

    I'm developing a theme that features custom type posts called "projects" and regular posts which are displayed on the blog page.

    On the homepage I'd like to display only the "project" custom type posts that are in the category "featured". I'd like this to paginate as I want to use infinite scroll.

    I have read all the documentation and tried every single trick, but I can get this page to display the pagination and I can't figure out what I'm doing wrong.

    This is how I'm running my loop:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $temp = $featured; // assign ordinal query to temp variable for later use
    $featured = null;
    $featured = new WP_Query(array( 'post_type' => 'projects', 'category__in'=> 14, 'post_status' => 'publish', 'posts_per_page' => 1, 'paged' => $paged)); ?>
    
    if ( $featured->have_posts() ) while ( $featured->have_posts()) : $featured->the_post();
    
    <div id="content">
    </div>
    
     <?php endwhile; ?>
     <?php wp_reset_postdata(); // reset the query ?>  
    
    <div class="pagination">
        <span class="pag-next"><?php next_posts_link(); ?> </span>
        <span class="pag-previous"><?php previous_posts_link(); ?></span>
    </div>

    Apologies, if this is not the right place where to post this question. I tried the WP_Advanced forum but it said only moderators can post there (?)

    Any help would be much appreciated!

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

  3. xrayeye
    Member
    Posted 1 year ago #

    Hi alchymyth,

    Thanks so much for your reply. That helped as the pagination is now showing, but it's stuck always on the first page.

    This how my code is looking right now:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $temp = $featured; // assign ordinal query to temp variable for later use
    $featured = null;
    $featured = new WP_Query(array( 'post_type' => 'projects','category__in'=> 14, 'post_status' => 'publish', 'posts_per_page' => 2, 'paged' => $paged)); ?>
    
    if ( $featured->have_posts() ) while ( $featured->have_posts()) : $featured->the_post();
    
    <div id="content">
    </div>
    
     <?php endwhile; ?>
     <?php wp_reset_postdata(); // reset the query ?>  
    
    <div class="pagination">
    <span class="pag-next"><?php next_posts_link('Older Entries ยป', $featured->max_num_pages); ?> </span>
    <span class="pag-previous"><?php previous_posts_link('Newer Entries', $featured->max_num_pages); ?></span>
    </div> 
    
    $temp = $featured; // assign ordinal query to temp variable for later use
    $featured = null;

    Maybe it has something to do with the way I'm running the loop on other pages. There's a projects page where I'm displaying thumbnails of all the project posts like this:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    <?php $projects_query = new WP_Query(array( 'post_type' => 'projects','post_status' => 'publish', 'posts_per_page' => 9, 'paged' => $paged));
    while($projects_query->have_posts()) : $projects_query->the_post(); ?>
        <div id="content">
    
        </div>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); // reset the query ?>  
    
    <div class="pagination">
        <span class="pag-next"><?php next_posts_link(); ?> </span>
        <span class="pag-previous"><?php previous_posts_link(); ?></span>
    </div>

    And the blog page where I display regular posts looks like this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $temp = $journal; // assign ordinal query to temp variable for later use
    $journal = null;
    $journal = new WP_Query(array( 'post_type' => 'post','post_status' => 'publish', 'posts_per_page' => 3,'paged' => $paged ));
    
    if ( have_posts() ) while ( have_posts() ) : the_post(); 
    
    <div id="content">
    </div>
    
    <?php endwhile; // end of the loop. ?>
    <?php wp_reset_postdata(); // reset the query ?>  
    
    <div class="pagination">
        <span class="pag-next"><?php next_posts_link(); ?> </span>
        <span class="pag-previous"><?php previous_posts_link(); ?></span>
    </div>

    I'm really lost here. I can't pin down what's the mistake I'm making!

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    This how my code is looking right now:

    can you post the full code of the template?

    (please use the pastebin as described in http://codex.wordpress.org/Forum_Welcome#Posting_Code )

    is there possibly another query before that, which might be messing with the query string, i.e with the 'paged' information?

Topic Closed

This topic has been closed to new replies.

About this Topic