There are numerous tutorials on the web about showing Custom Post types on Homepage. WordPress by default shows only Posts on the homepage. Custom Post types are not automatically shown on the homepage or the feed. Now while searching for the method you must have seen the following code everywhere:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] )
$query->set( 'post_type', array( 'post', 'page', 'album', 'movie', 'quote', 'attachment' ) );
return $query;
}
This code you can put in either your theme’s function.php file or a custom plugin file you created for your site. This code is all good but not many sites will tell you that this might break just any other loop that you might be using on your homepage. For instance if your homepage is showing a custom menu, then they will stop showing. This code infact converted all my custom loop widgets to use this very loop. So practically all widgets started showing the same posts as were shown on the homepage and my custom menu disappeared.
So my suggestion is to not to use this code to show custom post types on your homepage. Infact the code to do is even simpler but you will have to modify your home.php or index.php file whichever one you use for showing your homepage. Find the following line in your home.php/index.php
if (have_posts) :
and just before it add this line
query_posts ( array( 'post_type' => array('post', 'page', 'album', 'movie', 'quote', 'attachment') ) );
This code will modify the loop to show all these post types as well. Well if you don’t want to modify your core theme, you can always create a child theme and copy all the code from your theme’s index.php/home.php and add the above line in it and save in the child theme folder. This will preserve your original theme and your homepage will be over riden with the custom index.php/home.php in your child theme’s folder.
Update: Don’t forget to reset the query by using the below code:
wp_reset_query();
just after endif; in your home.php/index.php or some other query on your homepage might get messed.
February 10, 2012 at 7:45 am
I’m actually using WP_Query() and then calling wp_reset_postdata(), which doesn’t destroy the last query like wp_reset_query() does. I suppose it depends on what you’re doing, but this process doesn’t interfere with other calls.