I have this code on a query I'm doing

<?php query_posts('post_type=vendors'); ?>
<?php while (have_posts()):the_post(); ?>
$featured = get_field('promote_to_homepage', $post -> ID, true) ;                   
<?php if($featured){ ?>
<div id="vendor-img"><img src="<?php the_field('vendor_logo'); ?>"/></div><h5><?php  
the_title() ?></h5><?php } ?>
<?php endwhile; ?>

So what I would like to do is to show only one post, if I set the




the problem is that query will be always querying the latest post if I set to show 1, but what happens if the "promote_to_page" option is set on another post it will not shown.

Any ideas on how to querying all posts under that post type but only show 1 ?


  • By promote_to_page do you mean "Sticky posts"?
    – mrwweb
    Commented May 23, 2012 at 18:09

1 Answer 1


Here's a very basic example that should do what you need it to do. Basically, we get all of the posts in your custom post type, then loop through them individually. When we find the one set with "promote_to_homepage," we render some display and exit the loop:

// Query for all posts in the post type "vendors"
query_posts( 'post_type=vendors' );

$exitloop = false;

// Start the loop
while ( have_posts() ) : the_post();

    // Is this post the featured post?
    $featured = get_field( 'promote_to_homepage', $post->ID, true ) ;                   

    if ( $featured ) { ?>

    // This is our featured post, so display it and set $exitloop so we quit
    $exitloop = true;

        <div id="vendor-img">
            <img src="<?php the_field('vendor_logo'); ?>"/>
        <h5><?php the_title(); ?></h5>

    <?php }

    if ( $exitloop ) break;


There are more elegant ways to do this, though. There is no get_field() function in WordPress core, so I'm assuming you're using the Advanced Custom Fields plugin. If so, this field is stored in a meta key and can also be retrieved with:

get_post_meta( $post->ID, '_promote_to_homepage', true );

This means you can perform a custom query to select just 1 post that contains that particular meta key:

$args = array(
    'post_type'      => 'vendors',
    'posts_per_page' => 1,
    'meta_query'     => array( array(
        'key'     => '_promote_to_homepage',
        'value'   => true,
        'compare' => '='
    ) )

$custom_query = new WP_Query( $args );

// Start the loop
while ( $custom_query->have_posts() ) : $custom_query->the_post(); 

    <div id="vendor-img">
        <img src="<?php the_field('vendor_logo'); ?>"/>
    <h5><?php the_title(); ?></h5>

  • Yes sir! thanks a lot for your answer! It worked like charm. I was indeed, using advanced custom fields, sorry for not pointing that out. Best!
    – Jaypee
    Commented May 23, 2012 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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