Problems excluding a custom post-type from the loop

Morning all,

I’m having some issues with excluding my custom post-type ‘events’ from my index.php loop for my Blog page.

Read More

I simply want to display the posts from my actual Blog, which I assume come under the post-type ‘post’, but when I try to display the ‘post’ type it also shows my ‘events’ post-type.

Here’s my loop code:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <div class="individualPost">
                <h1 class="bottomBorder"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>

                 <ul class="blogMeta">
                     <li><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></li>
                     <li>Posted in <?php the_category(', '); ?></li>
                 </ul>
                 <?php if (has_post_thumbnail( $post->ID )): ?>
                    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumbs.php?src=<?php echo $image[0]; ?>&w=615&h=200&zc=1" alt="<?php the_title(); ?>" /></a>
                 <?php endif; ?>    
                 <!-- Display the Post's Content in a div box. -->
                   <?php the_excerpt(); ?>
            </div>
       <?php endwhile;?>
       <?php else : ?>

        <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
            <h1>Not Found</h1>
        </div>

       <?php endif; ?>

I’ve also tried this, with no luck:

<?php $loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 5 ) ); ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="individualPost">
                <h1 class="bottomBorder"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>

                 <ul class="blogMeta">
                     <li><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></li>
                     <li>Posted in <?php the_category(', '); ?></li>
                 </ul>
                 <?php if (has_post_thumbnail( $post->ID )): ?>
                    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumbs.php?src=<?php echo $image[0]; ?>&w=615&h=200&zc=1" alt="<?php the_title(); ?>" /></a>
                 <?php endif; ?>    
                 <!-- Display the Post's Content in a div box. -->
                   <?php the_excerpt(); ?>
            </div>
       <?php endwhile;?>

Thanks

Related posts

Leave a Reply

3 comments

  1. Try using the following before “if (have posts()..

    $args = array(
        'post_type' => 'post',
        'orderby'   => 'rand',
        'showposts' => '1'
    );
    query_posts( $args );
    
  2. No need to exclude the 'events' you just fetch the ‘post’ type.

    See the example –

    $args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        the_title();
        echo '<div class="entry-content">';
        the_content();
        echo '</div>';
    endwhile;
    

    it will fetch from 'post_type' => 'post', and number of page = 10.

    For more details scheck the link you will get some idea
    url: http://codex.wordpress.org/Post_Types

  3. Had the same issue, it can be probably related to the fact that you use a statc page as the homepage, and a “blog” page for the post stream.

    I solved putting this code before the loop:

    <?php query_posts("post_type=post"); ?>
    

    This works for me, but in order to correctly display your ‘events’ you need to create a archive-events.php with a plain loop without the query_post function.

    I have a “standard loop” in a loop.php that i call with

    <?php get_template_part( 'loop' ); ?>
    

    And I use the query_posts in the templates.

    i.e my inex.php presents

    <?php query_posts("post_type=post"); ?>
    <?php get_template_part( 'loop' ); ?>
    

    but my archive-mycustomposttype.php has only

    <?php get_template_part( 'loop' ); ?>
    

    Cheers