WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 */ 
share|improve this question
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
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
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
@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 Jan 30 '14 at 11:52

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
        //#do what you want with the post
share|improve this answer
Please explain why that would solve the issue. – toscho Oct 30 '12 at 12:43
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

i'm not exactly sure the full context of what you are doing here. there are a number of possible solutions.

first is to make sure you are accessing $post as you expect. in other words, make sure it is not being affected by other activity as WordPress builds the page.

i don't know if $post contains the object you expect at this point in the flow -- because just about anything could overwrite that. and from looking at your code, i don't even see where you assign $post for your own usage.

i noticed you are calling get_post_thumbnail_id($post->ID). so, i believe you may solve your problem by using $customPosts->$post->ID instead of $post->ID.

i consider Tidy's answer above to be a solution -- other than the suggestion of changing $post to $customPost

really, my answer is more generic, that is: make sure you do not cross variable names.

in some conditions your code may work, while in others conditions there may be a problem if other activity alters that variable. $post can be altered by something else in WordPress easily (obviously).

on a categories page for example, the posts will be generated by WordPress and it will utilize the global $post variable for itself. from my experiences, i would wager everything i've got on that being your problem. then suddenly your widget with a post loop breaks on those pages...

other things you can try in this situation or the future (but they must be called from with the loop:

the_post_thumbnail( array(100, 100) ); in your loop

or try $thumb = get_the_post_thumbnail( array(100, 100) );

share|improve this answer

When you created your custom post type, did you also add your categories to your new post type?

register_taxonomy_for_object_type( 'category', 'mycustomtype' );
share|improve this answer
i think we can assume he did since he said it was working in some cases but not on a categories page. – aequalsb May 18 '15 at 16:30

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(); 
share|improve this answer
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 May 29 '15 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. – Matthew T Jun 5 '15 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 Jun 5 '15 at 15:53
Fine, I can delete this answer if you like. – Matthew T Jun 5 '15 at 19:00

Your Answer


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.