Title: recent posts by tag(custom field)
Last modified: August 20, 2016

---

# recent posts by tag(custom field)

 *  Resolved [hrcwebmaster](https://wordpress.org/support/users/hrcwebmaster/)
 * (@hrcwebmaster)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-by-tagcustom-field/)
 * I am trying to list my recent posts by tag. The trick is that my “tag” is a custom
   field. I have tried about 13 different code combinations, from `<?php query>`
   to `$posts-array` and nothing seems to work. I have also browsed the forums and
   found others trying to do the same thing, but never with their custom field as
   the tag. I really don’t want to use a plugin if at all possible. Here’s the code
   I have gotten to work closest:
 *     ```
       <?php
   
           $args=array(
       	'numberposts' =>5,
       	'tag' => 'gary-alexander',
       	'orderby' =>'post_date',
       	'order' => 'DESC',
           );
           $my_query = new WP_Query($args);
           if( $my_query->have_posts() ) {
             echo 'Recent News:';
             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;
           } //if ($my_query)
         wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 * Of course, instead of “gary-alexander” I need to replace it with a call to the
   custom field. I know for sure that the code to call the correct custom field 
   is `<?php $key="member-name"; echo_get_post_meta($post->ID, $key, true); ?>`
   
   That code will without a doubt pull “gary-alexander” or whatever the person’s
   name I’m trying to pull. But when I plug this code into the code above after `'
   tag' =>` it displays nothing. I am SO CLOSE, any help is appreciated!!

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-by-tagcustom-field/#post-3019233)
 * Is your custom field in the Page that is calling this template? If so, try this:
 *     ```
       <?php
           $key = 'member-name';
           $tag = get_post_meta($post->ID, $key, true);
           $args=array(
       	'numberposts' =>5,
       	'tag' => $tag,
       	'orderby' =>'post_date',
       	'order' => 'DESC',
           );
       ```
   
 * You probably should add code to check if $tag actually has a value and if not,
   issue a warning that the custom field was not found.
 * EDIT: You are using ‘numberposts’ => 5, but no provision for pagination. If you
   want pagination, you need to add the ‘paged’ argument.
 *     ```
       <?php
           $key = 'member-name';
           $tag = get_post_meta($post->ID, $key, true);
           $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
           $args=array(
               'paged' => $paged,
       	'numberposts' =>5,
       	'tag' => $tag,
       	'orderby' =>'post_date',
       	'order' => 'DESC',
           );
       ```
   
 *  Thread Starter [hrcwebmaster](https://wordpress.org/support/users/hrcwebmaster/)
 * (@hrcwebmaster)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-by-tagcustom-field/#post-3019255)
 * Thanks! That first set worked (I did have to still include everything from `$
   my_query` forward, in case anyone is using this forum later).
    I don’t need a
   pagination because I have a full newsroom on another link, this is more of a “
   preview” of the recent posts. Thank you so much!!

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

The topic ‘recent posts by tag(custom field)’ is closed to new replies.

## Tags

 * ["recent post"](https://wordpress.org/support/topic-tag/recent-post/)
 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [tag](https://wordpress.org/support/topic-tag/tag/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [hrcwebmaster](https://wordpress.org/support/users/hrcwebmaster/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/recent-posts-by-tagcustom-field/#post-3019255)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
