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 would like to know if there's a way to do a query filtered by a custom field result. In this case, I have 6 posts I would like to retrieve, but only if they have "true" selected in a custom field I've previously created. The name of the custom field is called "featured".

Here's my code

<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=6');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <li>
        <?php if ( has_post_thumbnail() ) {?>
        <div class="post-thumb-140">
            <a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'post-thumb-140' ); ?></a> 
        </div> <!--post-thumb-->
        <?php } ?>

        <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    </li>
<?php endwhile; ?>

I've checked WordPress documentation on possible WP_Query arguments, but there are no filters based on a custom field.

Any ideas?

share|improve this question
7  
perhaps you missed this whole section? –  Milo Jan 6 '13 at 21:17
    
That's completely true, I was relating meta info to something else, my mistake. –  Johann Jan 6 '13 at 21:29

1 Answer 1

up vote 0 down vote accepted

Here's the corrected code, thanks @Milo!

<ul>

<?php
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=6&cat=1&meta_key=ticker&meta_value=yes');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <li>

    <?php if ( has_post_thumbnail() ) {?>
    <div class="post-thumb-140">
    <a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'post-thumb-140' ); ?></a> 
    </div> <!--post-thumb-->
    <?php } ?>

        <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    </li>
<?php endwhile; ?>

</ul>
share|improve this answer
    
Please accept your answer using the green tick to the left of your answer –  bungeshea_ Jan 7 '13 at 1:11

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.