• Resolved csleh

    (@csleh)


    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

Viewing 1 replies (of 1 total)
  • Thread Starter csleh

    (@csleh)

    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(

Viewing 1 replies (of 1 total)
  • The topic ‘Problem altering this code to get any tag, not just first one’ is closed to new replies.