Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Morning all,

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

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

share|improve this question
    
You don't need to exclude a custom post type from the query. Unless you explicitly specify that you want the custom post to be included by using 'post_type', the default 'post' type will be used. –  Rutwick Gangurde Oct 3 '11 at 8:39
    
This is what I thought, which is why I am confused as to why my query is still displaying the pages within my 'events' post-type –  remi90 Oct 3 '11 at 8:41
1  
I can't see anything wrong with the code you've posted - double check your custom post type is configured correctly. –  Jono Warren Oct 3 '11 at 9:07
    
@Jono +1. Double check if the custom post type is configured properly. Otherwise your loop code is fine. –  Rutwick Gangurde Oct 3 '11 at 9:33

3 Answers 3

up vote 0 down vote accepted

Try using the following before "if (have posts().."

$args = array(
    'post_type' => 'post',
    'orderby'   => 'rand',
    'showposts' => '1'
);
query_posts( $args );
share|improve this answer
    
Try not to use query_posts. Read here –  Rahil Wazir Dec 23 '13 at 20:59

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 s\check the link you will get some idea url: http://codex.wordpress.org/Post_Types

share|improve this answer
    
Sorry to be a pain guys, but I still can't seem to solve this issue. The blog page on my site is still displaying the 'events' custom post type in the loop. It definitely does look like it is a problem with my custom post type code rather than the loop's code, but the thing is I am using a plugin called CMS Press so I'm unsure how to gather the code for my Custom post type. Any ideas? –  remi90 Oct 13 '11 at 15:07

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.