Support » Fixing WordPress » List posts by tag use variable

  • Resolved raisedonindie


    I would like to grab a list of posts using tags to place within a post. However as this is in a template involving multiple pages I would like to set a variable using a custom field/value for the tag. Here is what I have thus far – the Unknown is where I need to place the variable. Any assistance would be very appreciated.

    $term = get_term_by('name','Unknown', 'post_tag');
      'tag__in' => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_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>
    wp_reset_query();  // Restore global post data stomped by the_post().

    This works if I place the actual tag in the Unknown spot, but I can’t seem to figure out how to utilize a variable in this instance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Assuming that you have a Custom Field named ‘tagname’ with a value of the name of the tag to select, I think this is what you want:

    $tagname = get_post_meta($post->ID,'tagname',true);
    $term = get_term_by('name',$tagname, 'post_tag');

    You should probably put in code to handle the case where the tagname Custom Field is missing or has an incorrect value.

    thanks for the reply vtxyzzy, I just fixed it a few minutes ago and chose to use a completely different method. I was attempting to pull by tag, now have chosen to implement a custom field instead. Here is what I did….

    <?php $artist = get_post_meta($post->ID, 'artist', true); ?>
    $my_query = new WP_Query('showposts=5&cat=1&meta_key=artist&meta_value=' . $artist. '');
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘List posts by tag use variable’ is closed to new replies.