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
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
4  
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
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 Jan 30 '14 at 11:52

3 Answers 3

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
7  
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
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. –  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

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 at 16:30

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

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.