Title: Get Posts by Custom Field
Last modified: August 19, 2016

---

# Get Posts by Custom Field

 *  Resolved [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * (@othellobloke)
 * [17 years ago](https://wordpress.org/support/topic/get-posts-by-custom-field/)
 * I’m trying to get posts with a custom field. Currently I’m using the PHP below
   which works but outputs ALL of them. I want to only output the latest post, but
   when I use the query posts in the first line it makes it disappear.
 *     ```
       <?php query_posts($query_string); query_posts('showposts=1'); ?>
       <?php if (have_posts()) : while (have_posts()) : the_post();
       $customField = get_post_custom_values("lead-story");
       if (isset($customField[0])) {
       //Custom field is set, display post info
   
       $values = get_post_custom_values("Image"); if ( $values != '' ) {
       echo '<a href="'; the_permalink(); echo '" rel="bookmark" title="Permanent Link to '; the_title(); echo '"><img src="'; echo get_option('home'); echo '/wp-content/uploads/'; echo $values[0]; echo '" alt="" id="leadpic" class="feature-img" /></a>'; } 
   
       echo '<a href="'; the_permalink(); echo '" rel="bookmark" title="Permanent Link to '; the_title(); echo '" class="lead-story-title">'; the_title(); echo '</a><br /><span class="smalltext">'; the_time('F jS, Y'); echo '</span><p>'; echo substr(get_the_excerpt(), 0, 150); echo '...<br /><a href="'; the_permalink(); echo '" rel="bookmark" title="Permanent Link to '; the_title(); echo '">Read More &raquo;</a></p>';
       }
       endwhile;
       endif;
       ?>
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * (@othellobloke)
 * [17 years ago](https://wordpress.org/support/topic/get-posts-by-custom-field/#post-1069553)
 * When I remove the query post tag in the first line… the posts show… but ALL of
   them which I don’t want.
 * Can anyone help please?
 *  Thread Starter [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * (@othellobloke)
 * [17 years ago](https://wordpress.org/support/topic/get-posts-by-custom-field/#post-1069673)
 * Anyone? Please?
 *  Thread Starter [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * (@othellobloke)
 * [17 years ago](https://wordpress.org/support/topic/get-posts-by-custom-field/#post-1069755)
 * SOMEONE?
 *  Thread Starter [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * (@othellobloke)
 * [17 years ago](https://wordpress.org/support/topic/get-posts-by-custom-field/#post-1069810)
 * For those who care I fixed this by changing the query string to:
 * <?php query_posts(‘showposts=1&meta_key=lead-story’); ?>

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Get Posts by Custom Field’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 1 participant
 * Last reply from: [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * Last activity: [17 years ago](https://wordpress.org/support/topic/get-posts-by-custom-field/#post-1069810)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
