show posts only on homepage but using custom post type and taxonomy for query posts via url

This is because query_posts replaces the original query with a new one (which includes all the listed post types).

For other reasons, you should not use query_posts!

For your purposes you can use the pre_get_posts hook. This is fired before the database is queried for posts. The ‘main query’ is that which is determined by the ‘url’ (i.e. is the url pointing to the front page, ?post_type= query vars etc).

By hooking into this, we can conditionally alter the query (i.e. if we’re on the front page then we can tell WordPress to get all the above post types).

query_posts on the other hand is fired only when the template has been loaded (so after WordPress has performed the ‘main query’). It throws this main query away and replaces it – wrecking havoc along the way (most notably pagination). If you don’t reset the query after using query_posts you can give yourself further headaches.

add_action('pre_get_posts','wpse57309_alter_front_page_query');
function wpse57309_alter_front_page_query( $query ){
     if( $query->is_main_query() && is_front_page() ){
         if( !$query->get('post_type') ){
             //post type is not set, the default will be an array of them:
             $query->set('post_type',array( 'movies', 'music', 'featued'));
         }
     }
}

You can then remove the query_posts from your template.

Keep in mind that the url www.example.com?post_type=xyz, will query posts of post type ‘xyz’ and (if the template) exists, will use archive-xyz.php as the template, according to the template hierarchy.

**Actually the !$query->get('post_type') conditional probably isn’t necessary -since if it is set, then WordPress will interpret it as a post type archive rather than the front page.*

Leave a Comment