WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Related Posts by Custom Taxonomy (16 posts)

  1. Kkumgyeol
    Member
    Posted 3 years ago #

    Hi, I've been using the code below to get related posts by tag. Now I'm looking for a way to instead get related posts by the custom taxonomy series. Is there a way to modify this to get there?

    <?php  //for use in the loop, list 5 post titles related to first tag on current post
    				$backup = $post;  // backup the current object
      				$tags = wp_get_post_tags($post->ID);
     				$tagIDs = array();
      				if ($tags) {
        				$tagcount = count($tags);
        				for ($i = 0; $i < $tagcount; $i++) {
          			$tagIDs[$i] = $tags[$i]->term_id;
    			    }
    			    $args=array(
    			      'tag__in' => $tagIDs,
    			      'post__not_in' => array($post->ID),
    			      'showposts'=>-1,
    			      'caller_get_posts'=>1
    			    );
    			    $my_query = new WP_Query($args);
    			    if( $my_query->have_posts() ) {
    			      while ($my_query->have_posts()) : $my_query->the_post(); ?>
    			        <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    			      <?php endwhile;
    			    } else { ?>
    			      <h2>No related posts found!</h2>
    			    <?php }
    			  }
    			  $post = $backup;  // copy it back
    			  wp_reset_query(); // to use the original query again
    			?>
  2. MichaelH
    Member
    Posted 3 years ago #

    Assume your taxonomy is called "genre", didn't test this but try:

    change this

    $tags = wp_get_post_tags($post->ID);

    to

    $tax_args=array('orderby' => 'none');
    $tags = wp_get_post_terms( $post->ID , 'genre', $tax_args);

    and change

    'tag__in' => $tagIDs,

    to

    'genre__in' => $tagIDs,
  3. Kkumgyeol
    Member
    Posted 3 years ago #

    This is weird. I've made the changes and the only pages that get related content pulled out are those that actually have been tagged in my 'series' taxonomy (the one you called genre). BUT, the links comes from all posts whether they have been tagged or not.

  4. MichaelH
    Member
    Posted 3 years ago #

    Well I'm getting the same 'problem'. Not sure if it's a bug or my logic so still researching...

  5. Kkumgyeol
    Member
    Posted 3 years ago #

    I really appreciate you taking your time with this!

  6. MichaelH
    Member
    Posted 3 years ago #

    Well 'genre__in' doesn't work. I thought it did based on http://core.trac.wordpress.org/ticket/9951 but I doesn't so will have to be something like:

    <?php
    //for in the loop, display all "content", regardless of post_type,
    //that have the same custom taxonomy (e.g. genre) terms as the current post
    $backup = $post;  // backup the current object
    $found_none = '<h2>No related posts found!</h2>';
    $taxonomy = 'genre';//  e.g. post_tag, category, custom taxonomy
    $param_type = 'genre'; //  e.g. tag__in, category__in, but genre__in will NOT work
    $post_types = get_post_types( array('public' => true), 'names' );
    $tax_args=array('orderby' => 'none');
    $tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
    if ($tags) {
      foreach ($tags as $tag) {
        $args=array(
          "$param_type" => $tag->slug,
          'post__not_in' => array($post->ID),
          'post_type' => $post_types,
          'showposts'=>-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(); ?>
            <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
            <?php $found_none = '';
          endwhile;
        }
      }
    }
    if ($found_none) {
    echo $found_none;
    }
    $post = $backup;  // copy it back
    wp_reset_query(); // to use the original query again
    ?>

    Note I left the $backup thing though I'm not sure it is needed

  7. Kkumgyeol
    Member
    Posted 3 years ago #

    This worked great. Thanks so much for your help!

  8. Dinho78
    Member
    Posted 3 years ago #

    Hmm...I'm trying this, and it kind of works. The original post however gets duplicated multiple times in the related posts section. Even if I limit it to only display 1 post. Any idea's what could cause this?

  9. homemrobo
    Member
    Posted 3 years ago #

    For those who had problens like dinho, try this:

    Where is

    "$param_type" => $tag->slug,

    change to:

    $param_type => $tag->slug,

  10. Arnii
    Member
    Posted 3 years ago #

    Cannot get this working... First of all, it lists the results two times. Second, it lists all posts with the right post-type, with no connection between tags (custom taxonomy)...

    If it's to any help, the code looks like this;

    <?php
    //for in the loop, display all "content", regardless of post_type,
    //that have the same custom taxonomy (e.g. genre) terms as the current post
    $found_none = '<h2>No related posts found!</h2>';
    $taxonomy = 'persona';// e.g. post_tag, category, custom taxonomy
    $param_type = 'persona'; // e.g. tag__in, category__in, but genre__in will NOT work
    $tax_args=array('orderby' => 'date');
    $tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
    if ($tags) {
    foreach ($tags as $tag) {

    $args=array(
    $param_type => $tag->slug,
    'post__not_in' => array($post->ID),
    'post_type' => 'live',
    'showposts'=> 4,
    '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(); ?>
    <h3>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></h3>
    <?php $found_none = '';
    endwhile;
    }
    }
    }
    wp_reset_query(); // to use the original query again
    ?>

  11. Arnii
    Member
    Posted 3 years ago #

    Sorry,

    <?php
    		//for in the loop, display all "content", regardless of post_type,
    		//that have the same custom taxonomy (e.g. genre) terms as the current post
    		$found_none = '<h2>No related posts found!</h2>';
    		$taxonomy = 'persona';//  e.g. post_tag, category, custom taxonomy
    		$param_type = 'persona'; //  e.g. tag__in, category__in, but genre__in will NOT work
    		$tax_args=array('orderby' => 'date');
    		$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
    			if ($tags) {
    				foreach ($tags as $tag) {
    
        			$args=array(
        			$param_type => $tag->slug,
        			'post__not_in' => array($post->ID),
        			'post_type' => 'live',
        			'showposts'=> 4,
        			'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(); ?>
        		<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
        		<?php $found_none = '';
        		endwhile;
        		}
        	}
        }
        wp_reset_query(); // to use the original query again
        ?>
  12. Nio
    Member
    Posted 3 years ago #

    This worked great for me to, but then I try to make a shortcode that didn't work anymore.

    I put this code in my theme folder into function.custom.php, and call it in the post with shortcode [relatedseries] like this

    function series_related() {
    	return show_related_here();
    }
    add_shortcode('relatedseries', 'series_related');
    
    function show_related_here() {
    
    //for in the loop, display all "content", regardless of post_type,
    //that have the same custom taxonomy (e.g. genre) terms as the current post
    $backup = $post;  // backup the current object
    $found_none = 'No related posts found!';
    $taxonomy = 'seria';//  e.g. post_tag, category, custom taxonomy
    $param_type = 'seria'; //  e.g. tag__in, category__in, but genre__in will NOT work
    $post_types = get_post_types( array('public' => true), 'names' );
    $tax_args=array('orderby' => 'none');
    $tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
    if ($tags) {
      foreach ($tags as $tag) {
        $args=array(
          "$param_type" => $tag->slug,
          'post__not_in' => array($post->ID),
          'post_type' => $post_types,
          'showposts'=>-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(); ?>
    <ol>
    <li>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </li>
    <li> test </li>
    </ol>
            <?php $found_none = '';
          endwhile;
        }
      }
    }
    if ($found_none) {
    echo $found_none;
    }
    $post = $backup;  // copy it back
    wp_reset_query(); // to use the original query again
    
    }

    What did I do wrong!?

  13. navi17
    Member
    Posted 3 years ago #

    Hi,

    I am using below code to display particular posts based on custom taxonnomy.

    <?php
    		//for in the loop, display all "content", regardless of post_type,
    		//that have the same custom taxonomy (e.g. genre) terms as the current post
    		$found_none = '<h2>No related posts found!</h2>';
    		$taxonomy = 'persona';//  e.g. post_tag, category, custom taxonomy
    		$param_type = 'persona'; //  e.g. tag__in, category__in, but genre__in will NOT work
    		$tax_args=array('orderby' => 'date');
    		$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
    			if ($tags) {
    				foreach ($tags as $tag) {
    
        			$args=array(
        			$param_type => $tag->slug,
        			'post__not_in' => array($post->ID),
        			'post_type' => 'live',
        			'showposts'=> 4,
        			'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(); ?>
        		<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
        		<?php $found_none = '';
        		endwhile;
        		}
        	}
        }
        wp_reset_query(); // to use the original query again
        ?>

    But i have one problem.
    I have hierarchy structure for categories.
    e.g main category-> sub category1->subcategory2
    i have posts in each subcategory lets suppose one in sub category1 and one in subcategory2
    But the problem is that when i click on main category it only displays one post but in fact there are two posts under it.
    when i click on sub category the posts displays fine.

    Can anyone help?
    Thanks
    navi

  14. fcarthy
    Member
    Posted 3 years ago #

    This is what I'm using and it retrieves the posts great. The only problem is that it retrieves up to 3 related posts PER term, not 3 total. Any ideas how to achieve this?

    <?php
    global $post;
    $terms = get_the_terms( $post->ID , 'badge', 'string');
    $do_not_duplicate[] = $post->ID;
    $postnum = 0;
    
    if(!empty($terms)){
        foreach ($terms as $term) {
            query_posts( array(
            'badge' => $term->slug,
            'showposts' => 3,
            'caller_get_posts' => 1,
            'post__not_in' => $do_not_duplicate ) );
            if(have_posts()){ ?>
    
                <?php while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
    		<?php $postnum++; ?>
    	<div class="single-small-posts-<?php echo $postnum; ?>">
          			<?php
    			$clipping_image = get_post_meta( $post->ID, 'clipping_image', true );
    			$terms_of_post = get_the_term_list( $post->ID, 'type', '','', '', '' );?>
    
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title();?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $clipping_image ?>&h=140&w=140&zc=1"></a>
    				<p class="single-small-post-cat"><?php if( $terms_of_post ) echo $terms_of_post; ?></p><p class="single-small-post-number">#<?php echo get_post_meta($post->ID,'incr_number',true); ?></p>
    
    		</div>
            		<?php $found_none = '';
         			endwhile; wp_reset_query();
    ?>
    
        <?php }
      }
    }
    if ($found_none) {
    echo $found_none;
    }
    $post = $backup;  // copy it back
    wp_reset_query(); // to use the original query again
    ?>
  15. rahy
    Member
    Posted 3 years ago #

    I know this has been closed, but it seems that some people still have problem with duplicating items.
    I use this codes and it works fine for me (milage may differ?)
    I modify a little from Theme "Jenny" (thanks to the author).

    # Sorry, I made a mistake in the codes I copied, so I delete the code until I resolve it.

  16. Mentores
    Member
    Posted 3 years ago #

    the solution from MichaelH worked very well!

    thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic