2

I'm having some trouble feeding a custom post-type I created (events) on my homepage. I am trying to allow this particular post-type to display under the query which looks for category 8 (Featured).

I have managed to enable to ability to add categories to my custom post type, but for some reason none of the posts from my custom post-type actually display within the feed, only my standard posts.

I currently have the post-type as POSTS but I have tried changing this to PAGES and no luck.

I have a feeling that this could be a problem with the loop rather than the code within my functions.php, because I actually have another loop on the same homepage which seems to be feeding my events custom post-type.

Here is the loop I'm using which doesn't work:

<?php
                    $featuredPosts = new WP_Query();
                    $featuredPosts->query('showposts=5&cat=8');
                    for($i=1; $i<=$featuredPosts; $i++) { // second for() loop for post slides
                        while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); // loop for posts
                    ?>
                    <li id="slide-<?php echo $i++; ?>" class="clearfix">

                        <div class="thumb clearfix">
                            <?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=150&h=150&zc=1" alt="<?php the_title(); ?>" /></a>
                            <?php endif; ?> 
                        </div>
                        <div id="featuredPanelText">
                            <div class="postsnip">
                                <h2><a href="<?php the_permalink(); ?>"><?php echo substr($post->post_title,0,30); // short title ?>...</a></h2>
                                <?php the_excerpt(); ?> 
                            </div>
                        </div>
                    </li>

                    <?php endwhile;
                    } // end for() loop number 2
                ?>

Here is the PHP snippet I have found and used in my functions.php:

    add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

    if ( ( is_home() || is_tag() ) && empty( $query->query_vars['suppress_filters'] ) )
        $query->set( 'post_type', array( 'post', 'events' ) );

    return $query;
}

Any help would be great, thanks guys!

2 Answers 2

1

Replace this line:

$featuredPosts->query('showposts=5&cat=8');

with

$featuredPosts->query(array('showposts' =>5, 'cat' =>8,'post_type' => array('post','events')));
3
  • This code suggested by @Bainternet should do the trick. Your problem comes in because with your query you're only requesting posts with the post type of 'post'. By splitting your query into an array, you're able to specify multiple post types (both 'post' and 'events'). Commented Sep 14, 2011 at 18:53
  • Thanks @Bainternet but I've actually just edited the file and it still doesn't seem to be loading my events post-type with the Featured category. I will try to have another look tonight, but if you have any other ideas that would be great
    – remi90
    Commented Sep 14, 2011 at 20:42
  • Sorry for the double post, here is the full code for my feed: link
    – remi90
    Commented Sep 14, 2011 at 20:46
1

Seem to have solved this with the following code:

<div id="featuredEvents">
    <h1 class="mainSectionHeader">Featured Events</h1>
    <?php $eventsFeed = new WP_Query(array('post_type' => 'events', 'posts_per_page' => 2 )); ?>
    <?php while ($eventsFeed->have_posts() ) : $eventsFeed->the_post(); ?>
    <div id="featuredOne">
        <?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=150&h=150&zc=1" alt="<?php the_title(); ?>" /></a>
        <?php endif; ?> 
        <div class="featuredText">
            <h2 class="redSubHeader"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
        </div>
    </div>
    <?php endwhile;?>
    <div style="clear:both;"></div>
</div>

Thanks for all you help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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