WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] List posts by tag + use variable (3 posts)

  1. raisedonindie
    Member
    Posted 3 years ago #

    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.

    <?php
    $term = get_term_by('name','Unknown', 'post_tag');
    $args=array(
      '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>
        <?php
      endwhile;
    }
    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.

  2. vtxyzzy
    Member
    Posted 3 years ago #

    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:

    <?php
    $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.
    `

  3. raisedonindie
    Member
    Posted 3 years ago #

    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); ?>
    <?php
    $my_query = new WP_Query('showposts=5&cat=1&meta_key=artist&meta_value=' . $artist. '');

Topic Closed

This topic has been closed to new replies.

About this Topic