Forums

[resolved] Listing recent posts in sidebar using a custom field (8 posts)

  1. FlossieT
    Member
    Posted 2 years ago #

    Having tried various plugins and searches, I'm stumped (I'm fairly new to the Loop...). I've been looking at WP_Query and query_posts mainly.

    I need a modified version of the 'recent posts' list in the sidebar, to show only longer posts (excluding some very short posts).

    We have a custom field that is populated for each of the longer posts that we want to show up, but the value will be different for each post.

    How do I show only the ten most recent posts where that custom field is NOT empty?

    It seems such a simple thing to do and yet I'm failing miserably - any pointers to tutorials that will walk me through this would be very much appreciated.

    Thanks,
    Rachael

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    <?php
    $args=array(
      'meta_key'=>'your_key_here',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 10,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of 10 recent Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    See query_posts() for more arguments.

  3. FlossieT
    Member
    Posted 2 years ago #

    MANY thanks. Now I just need to pull it apart again so I can understand how/why it works...

    It does take a long time to load - are there more efficient methods, or is it going to take extra time by its very nature (because of having to loop through the conditions)?

  4. FlossieT
    Member
    Posted 2 years ago #

    OK, now having trouble with a different bit of the same thing!

    Instead of the link text being the post's title, I want it to be the value of the same custom field we're evaluating to generate the lists.

    I'm looking at get_post_custom_values, but am obviously getting the syntax completely wrong - so where you have:

    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;

    I have:

    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li><?php the_author(); ?><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php get_post_custom_values('my-custom-field-key'); ?></a></p>
        <?php
      endwhile;

    but nothing displays.

    Sorry, I know this is really elementary...

  5. MichaelH
    Volunteer
    Posted 2 years ago #

    From my note:
    If custom field is present, formulate and display link from meta value

    <?php
    $link = get_post_meta($post->ID, 'link_url', true);
    $text = get_post_meta($post->ID, 'link_text', true);
    if ($link){
    echo 'Credit: <a href="'.$link .'">'.$text.'</a>';
    }
    ?>
  6. FlossieT
    Member
    Posted 2 years ago #

    Thanks. I've been knocking this around, and I can't get it to display anything where the text should be.

    The custom field is not empty, because the evaluation is successful.

    Yet when I use get_post_meta to retrieve the field contents and try to echo it to the browser, it's outputting nothing.

    If I change 'true' to 'false' above, it echoes 'Array' - which suggests to me it isn't finding any data. But I can't see how that's possible if it's passed the evaluation.

    Here's my code in full:

    <?php
    $args=array(
      'meta_key'=>'custom-field-key',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 10,
      'caller_get_posts'=> 1
    );
    $customflag = null;
    $customflag = new WP_Query($args);
    
    if( $customflag->have_posts() )
    {
      echo '<li id="recent-custom-posts" class="widget widget_recent_entries"><h2 class="widgettitle">Recent Posts</h2> <ul>' ;
      while ($customflag->have_posts()) : $customflag->the_post(); ?>
        <li><?php the_author(); ?>:
        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
        <?php
        $customflagtext = get_post_meta($post->ID, "custom-field-key", false);
    	if($customflagtext !== '') {
    	echo $customflagtext;
    	} ?>
    </a></li>
        <?php
      endwhile;
      echo '</ul></li>' ;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    This outputs a list of ten posts, each in the format 'Author: Array' (where Array is linked to the correct post).

    If I put 'false' back to 'true' to retrieve (supposedly) a single string, I get nothing.

    What am I missing? How can a field be not-empty and empty all at the same time?

  7. MichaelH
    Volunteer
    Posted 2 years ago #

    See Function_Reference/get_post_meta for the discussion on that template tag.

    Related:
    Custom Fields

  8. FlossieT
    Member
    Posted 2 years ago #

    Thanks - I've gone through all of that documentation, and much of the stuff that's linked to it, and yet I'm still getting nothing.

    I'll mark the original issue resolved and start a more specific thread!

Topic Closed

This topic has been closed to new replies.

About this Topic