5

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;   
9
  • 2
    There's no reason to use wp_reset_query when you're using a new WP_Query object. Commented Oct 29, 2012 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
    Commented Oct 29, 2012 at 23:59
  • 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
    Commented Dec 26, 2012 at 20:12
  • 5
    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? Commented Jun 29, 2013 at 21:48
  • 1
    @KrzysiekDróżdż 'posts_where' should not be the problem, because OP says that he had tried 'get_posts'that function suppress filters as default, so 'posts_where' filters aren't taken into account. So my guess is there is a filter on 'pre_get_posts' that not check for $query->is_main_query().
    – gmazzap
    Commented Jan 30, 2014 at 11:52

2 Answers 2

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
    }
7
  • 7
    Please explain why that would solve the issue.
    – fuxia
    Commented Oct 30, 2012 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
    Commented Oct 30, 2012 at 12:49
  • 1
    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.
    – Janine
    Commented Nov 1, 2012 at 16:34
  • Here's an good explanation when to use which type of query: wordpress.stackexchange.com/questions/1753/…
    – david
    Commented Nov 2, 2012 at 11:07
  • @toscho get_posts uses 'suppress_filters' = true by default, so if the problem was any filter on 'posts_where' using get_posts could solve the problem... but seems it's not the case.
    – gmazzap
    Commented Jan 30, 2014 at 11:49
0

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(); 
4
  • You've copied code from the question and added two parameters that are not mentioned in the question, or in comments. How exactly is that supposed to solve the problem? This is the second questionable answer you've posted in a very short time. Please review hoe to answer
    – s_ha_dum
    Commented May 29, 2015 at 16:20
  • It's not the array part that was the issue, that's done correctly. The bottom part of the code I shared, $the_query section is what helped make WordPress properly filter the custom post type by category. This answer resolved the other question I posted in as well. I tried a lot of different things and this is what got it working. That's how it helps. Commented Jun 5, 2015 at 15:34
  • The code posted is the functional equivalent of the code in the question but you've added parameters that are not part of the question. This does not solve this question but addresses a similar problem of your own.
    – s_ha_dum
    Commented Jun 5, 2015 at 15:53
  • Fine, I can delete this answer if you like. Commented Jun 5, 2015 at 19:00

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.