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 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' ).

    <?php 
// 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; ?>
share|improve this question
    
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
add comment

2 Answers

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

share|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
add comment

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(); 
share|improve this answer
add comment

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.