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="https://wordpress.stackexchange.com/questions/28523/<?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

Replace this line:

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

with

$featuredPosts->query(array('showposts' =>5, 'cat' =>8,'post_type' => array('post','events')));

Leave a Reply

Your email address will not be published. Required fields are marked *