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 have following query to select posts by my custom post-type. This works fine while being on the frontpage (the snippet is included in the sidebar). As soon it gets executed on a category page the post_type attribute is ignored and posts of the type "post" are returned. I guess this has to do with multiple loops but I have also tried to call wp_reset_postdata(); and wp_reset_query(); before and after.

$customPosts = new WP_Query( array( 
    'post_type' => 'mycustomtype', 
    'posts_per_page' => 12, 
    'orderby' => 'date', 
    'order' => 'DESC', 
    'post_status' => 'publish'
));

while ( $customPosts->have_posts() ) : $customPosts->the_post(); 
    $thumbnailUrl = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), "thumbnail");
    if ( !empty($thumbnailUrl)) {
      /* echo result */ 
    }   
endwhile;   
share|improve this question
2  
There's no reason to use wp_reset_query when you're using a new WP_Query object. –  Brian Fegter Oct 29 '12 at 23:16
    
Was just trying to get closer to a solution/workaround. Of course I was also trying without that statement. Removed them from the example to avoid confusion. –  david Oct 29 '12 at 23:59
    
Obviously it works when using a direct DB query such as "$wpdb->get_results ...". Would prefer a standard WP-Tag solution though. –  david Nov 2 '12 at 11:10
1  
I copy/paste your code into the category.php file of a wordpress instance where I have a custom post enabled, it's working perfectly. –  Simon Dec 26 '12 at 20:12
2  
This code should work fine. There are 2 reasons it could not work: 1. You have some typo in post type name. 2. There is some filter that is changing query (pre_get_posts, posts_where, etc.) Could you print_r $customPosts just before while and show me its content? –  Krzysiek Dróżdż Jun 29 '13 at 21:48
show 3 more comments

1 Answer

Have you tried using get_posts() instead?

 //#get access to post settings
    global $post;
    //#set parameters for extra loop
    $args = array(
    'post_type' => 'mycustomtype', 
    'posts_per_page' => 12, 
    'orderby' => 'date', 
    'order' => 'DESC', 
    'post_status' => 'publish'
    );
    //#get posts 
    $customPosts = get_posts($args);
    //#loop through them
    foreach($customPosts as $post)
    {
        //#set all the loop functions to use data from this post
        setup_postdata($post);
        //#do what you want with the post
    }
share|improve this answer
4  
Please explain why that would solve the issue. –  toscho Oct 30 '12 at 12:43
1  
This was indeed used in the first implementation but as toscho outlined it's not working as get_posts makes also use of WP_Query. So internally it should be the same. But thanks for trying to help! –  david Oct 30 '12 at 12:49
    
Sorry, quite new to this and did not get notified of these comments. I just posted the get_posts approach as that is the one I tend to use and it has always worked out for me. There are a few approaches to multiple loops in WP and while in theory they tend to come down to WP_Query internally, the results can be odd and differ depending on what else you have going on in the template. –  Tidy Nov 1 '12 at 16:34
    
Here's an good explanation when to use which type of query: wordpress.stackexchange.com/questions/1753/… –  david Nov 2 '12 at 11:07
    
Thanks for that handy link David –  Tidy Nov 2 '12 at 15:12
show 1 more 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.