Forums

[resolved] Problem altering this code to get any tag, not just first one (2 posts)

  1. csleh
    Member
    Posted 8 months ago #

    This code for getting related posts with the same first tag is brilliant, but now I'm trying to get it to work with any tag, not just the first one. This matches the first tag:

    <?php
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
      $first_tag = $tags[0]->term_id;
      $args=array(  'tag__in' => array($first_tag),  'caller_get_posts'=>1  );
      $my_query = new WP_Query($args);   if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?><?php the_title(); ?>
          <?php  endwhile;
      } wp_reset_query();
     } ?>

    I've tried replacing the first bit with variations on this, but keep getting server errors:

    $tags = wp_get_post_tags($post->ID);
    		$taglist = '';
    		if ($tags) {
    			foreach ($tags as $tag) {
    				$taglist .= $tag->term_id . ',';
      $args=array(
        'tag__in' => array($taglist),

    ANy ideas on how to alter the first code example to match any tags in common? Thanks

  2. csleh
    Member
    Posted 8 months ago #

    I just realized what I was doing wrong. There was a missing } so it broke everything. Replaced the top part with this bit, and it works.

    <?php
    $tags = wp_get_post_tags($post->ID);
    	$taglist = '';
    	if ($tags) {
    		foreach ($tags as $tag) {
    		$taglist .= $tag->term_id . ',';
    	}
    	  $args=array(

Reply

You must log in to post.

About this Topic