I have the following query to extract posts from two different types. However, how can I then target them in the actual content display conditional code?

The if ($my_query->post_type == 'featured' ) does not work. I also tried if (post_type == 'featured' ).

// query posts
$my_query = new WP_Query
        ( array(
        'post_status' => 'publish',
        'post_type'   => array('featured', 'wpsc-product'),
        'orderby'     => 'menu_order',
        'order'       => 'ASC',
        'showposts'   => 50 )); 

while( $my_query->have_posts() ) : $my_query->the_post();
$fitemlink = get_post_meta($post->ID,'new_fitemlink',true); 

// display the content 

if ($my_query->post_type == 'featured' ) { 
echo get_image_path(get_post_meta($post->ID, 'slideimage_src', true));

} else if ($my_query->post_type == 'wpsc-product' ) { 
echo wpsc_the_product_image(); } 

endwhile; ?>
  • Try $post_type = get_post_type( get_the_ID() ); I'm wondering if inside the custom loop $post->ID is messed up somehow. – helgatheviking Jul 19 '13 at 13:33

$my_query->post_type refers to the post type of the query (in this case an array), and not the results. (Its recommended to use $my_query->get('post_type') too, but anyway...).

To get the current post's (in the loop) post type:

$post_type = get_post_type();

You can also use that function outside the loop by passing the post object / ID. See codex entry.

| improve this answer | |
  • Thanks for your suggestion. I updated my question but this is not working and nothing shows. Any Further suggestions? – Sol Jul 19 '13 at 13:03

I eventually solved this with a simple query instead

if ( 'featured' == get_post_type() )  { echo get_image_path(get_post_meta($post->ID, 'snbf_slideimage_src', true)); } else if ( 'wpsc-product' == get_post_type() ) { echo wpsc_the_product_image(); 
| improve this answer | |

You're asking for the post_type property of the query, when in fact you ought to instead ask for that property from the current post variable $post. In fact, this is exactly what get_post_type() does.

Alternatively, if your query specifies the post type, you might ask for the corresponding query_var (e.g. get_query_var("post_type");).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.