0

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?

2
  • 7
    perhaps you missed this whole section?
    – Milo
    Commented Jan 6, 2013 at 21:17
  • That's completely true, I was relating meta info to something else, my mistake.
    – Johann
    Commented Jan 6, 2013 at 21:29

1 Answer 1

0

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>
0

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.