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 this code on a query I'm doing

<?php query_posts('post_type=vendors'); ?>
<?php while (have_posts()):the_post(); ?>
<?php 
$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

&showposts=1

or

&posts_per_page=1

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 ?

Thanks.

share|improve this question
    
By promote_to_page do you mean "Sticky posts"? –  mrwweb May 23 '12 at 18:09

1 Answer 1

up vote 2 down vote accepted

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'); ?>"/>
        </div>
        <h5><?php the_title(); ?></h5>

    <?php }

    if ( $exitloop ) break;

endwhile;

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'); ?>"/>
    </div>
    <h5><?php the_title(); ?></h5>

<?php 
endwhile;
share|improve this answer
    
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 May 23 '12 at 20:13

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.