WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] recent posts by tag(custom field) (3 posts)

  1. hrcwebmaster
    Member
    Posted 1 year ago #

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

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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',
        );
  3. hrcwebmaster
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic