Avoid duplicate custom post type posts in multiple loops in Wordpress

Posted by christinaaa on Stack Overflow See other posts from Stack Overflow or by christinaaa
Published on 2011-03-03T22:15:13Z Indexed on 2011/03/03 23:25 UTC
Read the original article Hit count: 253

Filed under:
|

I am running two loops with a custom post type of Portfolio (ID of 3). The first loop is for Featured and the second is for the rest. I plan on having more than 3 Featured posts in random order. I would like to have the Featured ones that aren't displaying in the first loop to show up in my second loop. How can I set this up so there are no duplicate posts?

<?php
/*
Template Name: Portfolio
*/
get_header(); ?>

    <div class="section-bg">

        <div class="portfolio">

            <div class="featured-title">
                <h1>featured</h1>
            </div> <!-- end #featured-title -->

            <div class="featured-gallery">

                <?php
                    $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 3, 'cat' => 3, 'orderby' => 'rand' );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post();
                ?>

                    <div class="featured peek">
                        <a href="<?php the_permalink(); ?>">
                            <h1>
                                <?php 
                                $thetitle = $post->post_title; 
                                $getlength = strlen($thetitle); 
                                $thelength = 40;
                                echo substr($thetitle, 0, $thelength); 
                                if ($getlength > $thelength) echo '...'; ?>
                            </h1>
                            <div class="contact-divider"></div>
                            <p><?php the_tags('',' / '); ?></p>
                            <?php the_post_thumbnail('thumbnail', array('class' => 'cover')); ?>
                        </a>
                    </div> <!-- end .featured -->

                <?php endwhile; ?>

            </div> <!-- end .featured-gallery -->

            <div class="clearfix"></div>

        </div> <!-- end .portfolio -->

    </div> <!-- end #section-bg -->

    <div class="clearfix"></div>

    <div class="section-bg">

        <div class="portfolio-gallery">

            <?php
                $args = array( 'post_type' => 'portfolio', 'orderby' => 'rand');
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post();
            ?>

                <div class="featured peek">
                    <a href="<?php the_permalink(); ?>">
                        <h1>
                            <?php 
                            $thetitle = $post->post_title; 
                            $getlength = strlen($thetitle); 
                            $thelength = 40;
                            echo substr($thetitle, 0, $thelength); 
                            if ($getlength > $thelength) echo '...'; ?>
                        </h1>
                        <div class="contact-divider"></div>
                        <p><?php the_tags('',' / '); ?></p>
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail', array('class' => 'cover')); ?></a>
                    </a>
                </div> <!-- end .featured -->

            <?php endwhile; ?>

            <div class="clearfix"></div>

        </div> <!-- end .portfolio-gallery -->

        <div class="clearfix"></div>

    </div> <!-- end #section-bg -->

<?php get_footer(); ?>

If possible, could the answer outline how to implement it into my existing code? Thank you. :)

© Stack Overflow or respective owner

Related posts about php

Related posts about Wordpress