Sometimes I want to access one particular CPT to extract something from it, for example a custom field value:

$group = new WP_Query( array( 
    'post_type' => 'group',
    'p' => $group_id
) );
while ( $group->have_posts() ) : $group->the_post();
    $group_type = get_post_meta($post->ID, "group_type", $single = true);       
endwhile;

However the purpose of a loop is to access more than one element so I dislike using a loop for a single post. Is there a way to do exactly the same thing (accessing this custom field value) without using a loop?

3 Answers
3

how about get_post?

$post       = get_post( $p );
$group_type = get_post_meta( $post->ID, 'group_type', true );

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *