How to create a separate loop for Custom Post Types

So, I can see from the tutorial how to make a new "single" file for custompress, but its unclear to me how to make a secondary loop to display the custom posts.
Where does this come from? Do I have to make it from scratch?

I don't want the custom post type to be in the loop with regular blog articles.

Thanks for your help!


  • DavidM

    Hi Nick and first off, welcome to WPMU DEV!

    I'm not totally sure I understand your question so please feel free to correct me if that's the case. But you shouldn't really need a secondary loop for this. Making a copy of your theme's single.php, renaming it according to your custom post type (which the plugin assists with), should be sufficient.

    The loop within that particular file should only then be used for that particular post type, rather than regular Posts. You wouldn't need to worry about regular Posts being in that loop then.

    Does that help?


  • nickrosener

    Here's what I'm looking to do:

    I want to create a portfolio page using custom post types. I created a custom post type called "work." Yet, I don't want the "work" posts to show up alongside the blog articles in the regular loop.

    To remedy this, I want to create a clone of the wordpress loop that only displays posts of the "work" post type, and display that page as the "work" page.

    As I understand it, the single-work.php template file is meant to display a single work post, correct?

    I'm working on cloning the existing index.php file and trying to filter the posts to only include "work" posts, but I'm not having any luck so far.

    Any ideas?

    Thanks for your help.

  • fitoussi

    Hi Nike,
    to create a loop that displays posts from "work" post type try this:
    1) in the admin area create new page name "work" and leave it empty.

    2)you need to crate a template using file editor. name this file "page-work.php". this file should be in your theme folder. edit the file and paste this code:

    <?php get_header(); ?>
                <div id="content" >
    				<h1 class="entry-page-title">Work</h1>
                      <?php query_posts( array( 'post_status' => 'publish' , 'post_type' => array( 'work' )  ) ); ?>
                                       <!-- this is where you should insert your content of the posts -->
                                       <?php echo the_title(); ?>
                                       <?php echo the_excerpt(); ?>
                    <?php endwhile; ?>
                     <?php wp_reset_query(); ?>
                </div><!-- #content -->
    			<div class="clear"></div>
    <?php get_footer(); ?>

    inside the loop you can insert some other information.

    i hope it helps

  • nickrosener

    Hi Arun,

    I'm still tweaking this code. There were some syntax errors in what was provided but it got me going in the right direction. I'll post what I end up with in the forum.

    I would definately recommend adding functionality in this plugin to create a loop file like this. I would have expected it, as it seems to be an integral part of the custom post functionality.

    Having to write the loop file myself is pretty tedious.


  • nickrosener

    Hi Everyone,

    I don't know if this is helpful, but I've finished the customization of the loop for the client.

    This is exactly what I put up on the client site, including some customizations I made for the client.

    For example, I wanted to add the custom post tags to the loop.
    Also, I wanted to display custom fields in the loop, but only if they were in use. I would be great to have a drag-and-drop GUI that helps insert custom functionality into the site.

    I've attached my loop file.

    I hope it helps :slight_smile:


  • nickrosener

    Okay, the system didn't like that attachment: here's the code copied.

     * @package WordPress
     * @subpackage U-Design
     * Template Name: TNC Work Page Loop Template
    $content_position = ( $udesign_options['pages_sidebar'] == 'left' ) ? 'grid_16 push_8' : 'grid_16';
    <div id="content-container" class="container_24">
        <div id="main-content" class="<?php echo $content_position; ?>">
    	<div class="main-content-padding" style="padding:0;">
                      <?php query_posts( array( 'post_status' => 'publish' , 'post_type' => array( 'work' )  ) ); ?>
                      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="work_post">
                            <h2 style="padding-bottom: 10px;"><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php                                                         the_title(); ?></a></h2>
    <?php if( (get_the_term_list( $post->ID, 'services', 'Services Provided: ', ', ', '' )) || (get_post_meta( $post->ID, 'ct_clientwebs_text_d547', true )) || (get_post_meta( $post->ID, 'ct_partnerage_text_e86a', true )) ) { echo "<div class=\"work_metadata\">"; } ?>
    <?php if( (get_the_term_list( $post->ID, 'services', 'Services Provided: ', ', ', '' )) || (get_post_meta( $post->ID, 'ct_clientwebs_text_d547', true )) || (get_post_meta( $post->ID, 'ct_partnerage_text_e86a', true )) ) { echo "<p>"; } ?>
    <?php echo get_the_term_list( $post->ID, 'services', 'Services Provided: ', ', ', '' ); ?>
    <?php if(get_the_term_list( $post->ID, 'services', 'Services Provided: ', ', ', '' )){echo "<br/>";}?>
    <!-- Client Website -->
    <?php if ( get_post_meta( $post->ID, 'ct_clientwebs_text_d547', true ) ) : ?>
    Client Website: <a href="<?php echo get_post_meta( $post->ID, 'ct_clientwebs_text_d547', true ); ?>"><?php echo get_post_meta( $post->ID, 'ct_clientwebs_text_d547', true ); ?></a><br/>
    <?php endif; ?>
    <!-- End Client Website -->
    <!-- Partner Link -->
    <?php if ( get_post_meta( $post->ID, 'ct_partnerage_text_e86a', true ) ) : ?>
    Partner Agency: 
    <?php if ( get_post_meta( $post->ID, 'ct_partnerage_text_c506', true ) ) : ?>
    <a href="<?php echo get_post_meta( $post->ID, 'ct_partnerage_text_c506', true ); ?>">
    <?php endif; ?>
    <?php echo get_post_meta( $post->ID, 'ct_partnerage_text_e86a', true ); ?>
    <?php if ( get_post_meta( $post->ID, 'ct_partnerage_text_c506', true ) ) : ?>
    <?php endif; ?>
    <?php endif; ?>
    <!-- End Partner Link -->
    <?php if( (get_the_term_list( $post->ID, 'services', 'Services Provided: ', ', ', '' )) || (get_post_meta( $post->ID, 'ct_clientwebs_text_d547', true )) || (get_post_meta( $post->ID, 'ct_partnerage_text_e86a', true )) ) { echo "</p>"; } ?>
    <?php if( (get_the_term_list( $post->ID, 'services', 'Services Provided: ', ', ', '' )) || (get_post_meta( $post->ID, 'ct_clientwebs_text_d547', true )) || (get_post_meta( $post->ID, 'ct_partnerage_text_e86a', true )) ) { echo "</div><!-- Closes work metadata -->"; } ?>
    <?php if(has_post_thumbnail()){ 
    echo "<div class=\"post_thumbnail\">";
    if ( has_post_thumbnail() ) {
      the_post_thumbnail( 'medium');
    echo "</div><!-- Closes Post Thumbnail-->";
    global $more;    // Declare global $more (before the loop).
    $more = 0;       // Set (inside the loop) to display content above the more tag.
    the_content(the_title( "Read more about <em>", "</em>...", false )); ?>
    </div><!-- Closes Work-Post -->
                   <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
                     <?php wp_reset_query(); ?>
                </div><!-- #content -->
    			<div class="clear"></div>
    	</div><!-- end main-content-padding -->
    	global $udesign_options;
    	$sidebar_position = ( $udesign_options['pages_sidebar'] == 'left' ) ? 'grid_8 pull_16 sidebar-box' : 'grid_8';
    	<div id="sidebar" class="<?php echo $sidebar_position; ?>">
    	    <div id="sidebarSubnav" class="work_page">
    <ul class="work_page_sidebar">
    <?php dynamic_sidebar('custom-work-portflio'); ?>
    	    <!-- end sidebarSubnav -->
    	<!-- end sidebar -->
        </div><!-- end main-content -->
    </div><!-- end content-container -->
    <div class="clear"></div>
    <?php get_footer(); ?>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.