Wordpress: Add custom post-types to main query

Some days ago, i had a problem with my website and wordpress post-types. I added some custom post-types but couldnt show those posts on my homepage because by default, main post query just fetchs posts from post-type POST, not from all custom post-types. Finally, i fixed that issue with below extra code. The code should be added to function.php of your theme.

// Add custom post types to main query 
add_filter( 'pre_get_posts', 'my_get_post_types' ); 

function my_get_post_types( $query ) { 
    if ( is_home() && $query->is_main_query() )
    // add your custom post-type here 
    $query->set( 'post_type', array( 'post', 'videos', 'cheats', 'photos') ); 
    return $query;
}

Cheers.

Written on 28 January 2013

Share your thoughts