WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get Posts by Custom Field (5 posts)

  1. Ryan Paul
    Member
    Posted 5 years ago #

    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;
    ?>
  2. Ryan Paul
    Member
    Posted 5 years ago #

    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?

  3. Ryan Paul
    Member
    Posted 5 years ago #

    Anyone? Please?

  4. Ryan Paul
    Member
    Posted 5 years ago #

    SOMEONE?

  5. Ryan Paul
    Member
    Posted 5 years ago #

    For those who care I fixed this by changing the query string to:

    <?php query_posts('showposts=1&meta_key=lead-story'); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic