Custom Post Type Pagination & duplicate posts

I’m working on a template which will use some custom-post-types & custom-taxonomies.. I’m having problems with pagination and duplicate posts.

HOME.PHP

Read More

LOOP #1
On the homepage (home.php) I have 2 Loops. The fist one shows 1 post (custom post type: Projects) which has the custom-taxonomy “Featured” assigned to it.

LOOP #2
On the second loop I have the six most recent “Projects” (Custom post type) but I dont want a duplicate from the first loop. Under the six posts I’d like a pagination

Both loops in home.php work.. but I still have that duplicate and I can’t get pagination to work..

PasteBin

  • home.php: http://pastebin.com/6ac2asue

  • functions.php: http://pastebin.com/1SK206Bh

    <?php $do_not_duplicate = array();
    $folio_loop = new WP_Query( array
        (
            'featured' => 'featured-post',
            'post_type' => 'projects',
            'posts_per_page' => '1',
            )
        );
    
         while ( $folio_loop->have_posts() ) : $folio_loop->the_post();
        $do_not_duplicate[] = $post->ID; ?>
    
        <div id="featured" style="position: relative;">
    
    
            <div class="featured-overlay" style="position: absolute; right: 20px; bottom: 20px; width: 165px; height: 165px; text-align: center;">
                <div class="project-meta-featured">
              <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
            <span>
            <?php
                global $post;
                $text = get_post_meta( $post->ID, '_cmb_project_meta', true );
                echo $text;
            ?>
            </span>
            <br />
            <?php
                global $post;
                $text = get_post_meta( $post->ID, '_cmb_client_meta', true );
                echo $text;
            ?>
    
    
            </a>
              </div>
            </div>
    
            <?php if ( has_post_thumbnail()) : ?>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                    <?php the_post_thumbnail('featured'); ?>
                </a>
            <?php endif; ?>
    
    
    
        </div>
    
    <?php endwhile; ?>
    

Related posts

Leave a Reply

1 comment

  1. I think the best way to implement this is to use pre_get_posts hook. Take a look at this code

    function customize_query( $query ) {
        $post = get_posts(array(
             'post_type' => 'projects',
             'taxonomy' => 'featured',
             'numberposts' => 1
        ));
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'post_type', 'projects' );
            $query->set( 'posts_per_page', 6 );
            if($post && !empty($post))
                $query->set( 'post__not_in', array($post[0]->ID) );
        }
    }
    add_action( 'pre_get_posts', 'customize_query' );
    

    Then in your home page

    $post = get_posts(array(
         'post_type' => 'projects',
         'taxonomy' => 'featured',
         'numberposts' => 1
    ));
    // display the post just retrieved, it must be coming from cache
    
    // we can use the global query since now it contains the 6 posts we want
    while(have_posts()) :
        the_post();
        // display the post
    endwhile;
    

    You’ll need to modify it a lot but it might help you achieve what you want. In here the pagination should wotrk as normal since you just modified the main page query