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'm sure there's some very easy explaination for this, but I'm stuck. I am just trying to put a nice, simple recent posts element on the front-page of a site and for some reason when I try to use:

$args = array( 
    'post_type' => array('post','recipes'), 
    'posts_per_page' => 4
);
query_posts( $args );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    ...

endwhile; 
wp_reset_postdata(); 

it won't show the recipes. The strange thing is that if I call 'post' and 'page' it'll display both, but as soon as I add 'recipes' it just displays the first type requested that's not recipes. Am I missing something easy here?

Edit:

Updated with WP_Query instance instead of query_posts, still doesn't work.

$argz= array(
'post_type' => array('post','recipes'), 
'posts_per_page' => 4
);

$my_query = new WP_Query($argz);

Still doesn't work. Still returns the correct query var and everything, but doesn't display both post types.

share|improve this question
1  
are you 100% sure that a recipe falls within the four most recent posts/recipes? –  mrwweb May 21 '12 at 3:59
    
Yes I am - I published a test post, page and recipe and still have the issue. Also found out that if I have posts, pages and recipes only the first item in the array will display (posts) but if recipes isn't in there it displays both posts and pages like it should –  Rich May 21 '12 at 4:23
    
Just dotting the I's and crossing the T's :) Some more things to try. 1) Can you confirm that this is your full code snippet and it's not whittled down for example? 2) Have you double-checked the CPT name? 3) How are you showing the loop? A little more code after this snippet might show something else as the culprit. 4) I just noticed this. posts_per_page is supposed to take an integer not a string. Drop the quotes and try it again. –  mrwweb May 21 '12 at 4:47
    
Ok, updated code to be exact, and fixed the posts_per_page element. As for custom post name - if I take the post post type out of this statement it returns the 4 most recent recipes. This is the main loop for this page. –  Rich May 21 '12 at 15:48
add comment

4 Answers

This should work to query multiple post types:

query_posts('post_type[]=post&post_type[]=page&post_type[]=event');

You can even test it directly into your browser,

example.com/?post_type[]=post&post_type[]=page
share|improve this answer
add comment

I had this problem with querying on two custom post types, using WP_Query. I had no problem querying for one type or the other in the array, but not both at the same time.

Did not work:

$args = array( 
    'post_type' => array('custom_type_1','custom_type_2'), 
    'posts_per_page' => 4
);

Did work:

$args = array( 
    'post_type' => array('custom_type_1'), 
    'posts_per_page' => 4
);

Did work:

$args = array( 
    'post_type' => array('custom_type_2'), 
    'posts_per_page' => 4
);

Awfully confusing to say the least.

Solved the problem by making sure that both custom post types had the "Make 'Custom Post Type Name' translatable" checkbox for WPML. WPML must not like it if you try to query on two post types where one is translatable and the other is not.

(I'm on WordPress 3.4.1 and WPML 2.5.2 - latest as of the date of this post)

share|improve this answer
add comment

Wow. not sure of exactly what the culprit is (will dig further) but it seems that something about WPML was preventing the actual SQL query from requesting the custom post type in conjuction with standard posts and pages. When I disabled WPML it worked. Thanks for all the help everyone - I'm going to follow up on this issue once I have a more concrete idea of what has happened

share|improve this answer
    
WPML may be modifying the query with pre_get_posts or query_posts() and overriding your changes. IIRC, WPML creates a custom post type for some internal mappings, so that may be why they're modifying the post_type param. –  Ian Dunn May 22 '12 at 6:03
add comment

1. Don't use query_posts for secondary loops

This is just a general best practices recommendation and not the cause of your problem.

2. When you register the post type make sure 'public' => true,

3. Put the following in a template somewhere and report back the output. It will return the object of each registered custom post type and will verify whether or not your using correct name and if public is set to true.

$post_types = get_post_types( array( '_builtin' => false ), 'object' );
            foreach ( $post_types as $post_type ) {
                var_dump( $post_type );
            }
share|improve this answer
    
@Rich - search this site for examples of using the pre_get_posts action to modify the main query. when you call query_posts in a template, the query for that page has already happened, you're just discarding it and running another query. for this reason it's best to avoid query_posts altogether. –  Milo May 21 '12 at 16:01
    
@Chris_O - checked everything here - it is the primary loop, public is set to true and I did a var dump, revealing that thus. As I mentioned above, it's only post_type is set to array('post','recipes') that it doesn't work. If I just call 'recipes' it works beautifully! I'm so confused! –  Rich May 21 '12 at 16:04
    
@Milo - would it work better to use a new WP_Query? I know I'm going to have other loops on the page using WP_Query.. –  Rich May 21 '12 at 16:08
    
@Milo - I just realized that, in fact, I'm not using the main loop. duh. I modified the above code to use a new WP_Query –  Rich May 21 '12 at 16:23
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.