Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to do the following:

I have a index.php file in which I query all posts from the existing post_types. Like the code below.

 <?php if (is_home()); { ?>

    <?php
    global $wp_query;
    query_posts( array('post_type' => 
array( 'movies', 
'music', 'featured' ),'showposts' => 10, 'paged'=>$paged )
    );?>     

<?php }?>

I have a navigation menu e.g. "Movies" which url links to ?post_type=movies and so on.

But when I use it, everytime all posts are shown. When I delete the code above from the index.php everything works. Only the posts of a specific post_type are shown.

I only want to have all posts from all custom post types, when it is really the websites home url. How could I do it?

is there maybe an option to create a customposttype.php or so? like the taxonomy.php?

thank you!

AD


Hi Stephen, I have another question: I am getting the titles for each section by using this code here:´post_type; ?>´ in between h2 tags - but now on Startpage I get different names of the post type - but I'd like to have just: "Home" as Title... I tried it with is_home() and with is_front_page() but I had no luck. Do you have a hint? Thank you very much!!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
Hello Harris, thanks a lot for the reply! It's working great! Well - it shows post of the different post types - but I am not sure why it only shows a few posts from the post_types - not all. Seems to have something like a post limit - but I didn't set it to only 5... Thank you very, very much! AD –  ad2003 Jul 3 '12 at 14:44
    
The example doesn't alter the number of posts shown, so it might in your site's options? Otherwise you can always use $query->set('posts_per_page',10); for instance. –  Stephen Harris Jul 3 '12 at 14:46
    
Superb. I am deeply grateful for your help and magic! Thanks! AD –  ad2003 Jul 3 '12 at 14:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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