WordPress.org

Ready to get started?Download WordPress

Forums

List of posts using custom taxonomy term (8 posts)

  1. miradev
    Member
    Posted 3 years ago #

    I have a custom taxonomy. At the bottom of each post I want to list all posts that use the same term.

    MetaCode:

    apply_filter('the_content','addmylist');
    function addmylist($content){
      global $post;
      $term = get_post_term($post->ID, 'mytaxonomy');
      $extra = wp_page_list(array('include'=>get_postids_using_term($term, 'mytaxconomy')));
      return $content . $extra;
    }

    Is there a way of actually doing this?

  2. MichaelH
    Member
    Posted 3 years ago #

    Haven't used this for a while but might see if you can make it fit:

    <?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
    ?>
  3. miradev
    Member
    Posted 3 years ago #

    Thanks Michael. Won't that put me in an infite loop if I put it in the 'the_content' filter?

  4. MichaelH
    Member
    Posted 3 years ago #

    Not sure what would happen--of course that example is for use in a template such as index.php or a Page Template.

  5. miradev
    Member
    Posted 3 years ago #

    I was hoping to pop it in a plugin, to make it theme independent.

    It seemed such a simple thing when I started it!

  6. MichaelH
    Member
    Posted 3 years ago #

    Then look for Otto's PHP Code widget plugin and use that.

  7. miradev
    Member
    Posted 3 years ago #

    Thanks, bu that would still have theme dependence (i.e. a widgetised area)?

    I'll have a go at going the long route via get_objects_in_term() and get_posts()

  8. MichaelH
    Member
    Posted 3 years ago #

    There is nothing stopping you from putting that in a plugin. You might use a Shortcode or even filter on the_content.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.