0

I have a loop to show my custom post type of 'case_studies'. It originally showed all posts until I wanted to just show 3 specific posts. The problem is that it only shows the first post in my list of 'p=54,49,44'. What am I doing wrong?

$case_studies = new WP_Query();
                $count = 0; 
                $case_studies->query('post_type=case_studies&p=54,49,44');  
                while($case_studies->have_posts()) : $case_studies->the_post(); $count++;
                    $custom = get_post_custom($post->ID);
                    $case_studies_image_url = $custom['second_image'][0];
1
  • p parameter is for single post selection(just a specific one), if you need a group use post__in as suggested by Steve.
    – t31os
    Feb 4, 2011 at 12:14

1 Answer 1

3

I think you need to change:

$case_studies->query('post_type=case_studies&p=54,49,44');

to

$case_studies->query_posts( array( 'post__in' => array( 54, 49, 44 ) ) );

I'd look at this page for LOTS more examples. http://codex.wordpress.org/Function_Reference/query_posts. when your there search for "Multiple Posts/Pages Handling".

Hope this helps, I'm still new to wordpress.

7
  • You should really use get_posts() instead of query_posts(). "The query_posts() function is intended to be used to modify the main page loop only. It is not intended as a means to create secondary loops on the page. If you want to create separate Loops outside of the main one, you should use get_posts() instead. Use of query_posts() on loops other than the main one can result in your main loop becoming incorrect and possibly displaying things that you were not expecting." (codex.wordpress.org/Function_Reference/…) Feb 4, 2011 at 4:42
  • 2
    There is no WP_Query->query_posts() method, you are confusing it with query_posts() function. $case_studies->query() is correct.
    – Rarst
    Feb 4, 2011 at 6:29
  • 1
    @Steve Fischer - Nice answer. Feb 4, 2011 at 6:36
  • @Rarst & @MikeSchinkel I appologize. I mis-typed. I think I meant $case_studies = query_posts(..... Feb 4, 2011 at 14:12
  • @Steve Fischer your advice on post__in is accurate, but query_posts() is unfitting for secondary loop - WP_Query object as in question or get_posts() as per @tnorthcutt comment are appropriate.
    – Rarst
    Feb 4, 2011 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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