WP_Query ignores post_type in category view

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;   

Related posts

Leave a Reply

2 comments

  1. 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
        }
    
  2. This will resolve your issue, I was having the same problem, when I put this it worked perfectly.

    $args = array(
         'post_type' => 'mycustomtype', 
         'posts_per_page' => 12, 
         'orderby' => 'date', 
         'order' => 'DESC', 
         'post_status' => 'publish',
         'tag' => 'mycustomtag',
         'cat' => 'mycustomcategory'
        );              
    
    $the_query = new WP_Query( $args );
    if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();