Skip to main content
Fixed code error
Source Link

This should work for a custom taxonomy.

<?php
function get_related_posts() {

    global $post;
        
    $taxonomy = 'your_custom_taxonomy';
    $terms = wp_get_post_terms( $post->ID, $taxonomy );

    if($terms) {
        $term_arr = '';
        foreach( $terms as $term ) {
            $term_arr .= $term->slug . ','; // create array of term slugs
        }

        $args = array(
          'showposts' => 5, // you can change this to whatever you need
          'post__not_in' => array($post->ID) // exlude current post from results
          'tax_query' => array(
            array(
              'taxonomy' => $taxonomy,
              'field' => 'slug', // using 'slug' but can be either 'term_id', 'slug' or 'name'
              'terms' => $term_arr // array of ID values, slugs or names depending what 'field' is set to
            )
          )
        );
        $related_posts = get_posts( $args );

        if($related_posts) {
                
          foreach ( $related_posts as $post ) : setup_postdata( $post ); 
            // output whatever you want here
            the_title(); // for example
          endforeach;

        } else {

          echo 'No Related Posts';

        }

    }

    wp_reset_postdata();
}
?>

This should work for a custom taxonomy.

<?php
function get_related_posts() {

    global $post;
        
    $taxonomy = 'your_custom_taxonomy';
    $terms = wp_get_post_terms( $post->ID, $taxonomy );

    if($terms) {
        foreach( $terms as $term ) {
            $term_arr .= $term->slug . ','; // create array of term slugs
        }

        $args = array(
          'showposts' => 5, // you can change this to whatever you need
          'post__not_in' => array($post->ID) // exlude current post from results
          'tax_query' => array(
            array(
              'taxonomy' => $taxonomy,
              'field' => 'slug', // using 'slug' but can be either 'term_id', 'slug' or 'name'
              'terms' => $term_arr // array of ID values, slugs or names depending what 'field' is set to
            )
          )
        );
        $related_posts = get_posts( $args );

        if($related_posts) {
                
          foreach ( $related_posts as $post ) : setup_postdata( $post ); 
            // output whatever you want here
            the_title(); // for example
          endforeach;

        } else {

          echo 'No Related Posts';

        }

    }

    wp_reset_postdata();
}
?>

This should work for a custom taxonomy.

<?php
function get_related_posts() {

    global $post;
        
    $taxonomy = 'your_custom_taxonomy';
    $terms = wp_get_post_terms( $post->ID, $taxonomy );

    if($terms) {
        $term_arr = '';
        foreach( $terms as $term ) {
            $term_arr .= $term->slug . ','; // create array of term slugs
        }

        $args = array(
          'showposts' => 5, // you can change this to whatever you need
          'post__not_in' => array($post->ID) // exlude current post from results
          'tax_query' => array(
            array(
              'taxonomy' => $taxonomy,
              'field' => 'slug', // using 'slug' but can be either 'term_id', 'slug' or 'name'
              'terms' => $term_arr // array of ID values, slugs or names depending what 'field' is set to
            )
          )
        );
        $related_posts = get_posts( $args );

        if($related_posts) {
                
          foreach ( $related_posts as $post ) : setup_postdata( $post ); 
            // output whatever you want here
            the_title(); // for example
          endforeach;

        } else {

          echo 'No Related Posts';

        }

    }

    wp_reset_postdata();
}
?>
Source Link

This should work for a custom taxonomy.

<?php
function get_related_posts() {

    global $post;
        
    $taxonomy = 'your_custom_taxonomy';
    $terms = wp_get_post_terms( $post->ID, $taxonomy );

    if($terms) {
        foreach( $terms as $term ) {
            $term_arr .= $term->slug . ','; // create array of term slugs
        }

        $args = array(
          'showposts' => 5, // you can change this to whatever you need
          'post__not_in' => array($post->ID) // exlude current post from results
          'tax_query' => array(
            array(
              'taxonomy' => $taxonomy,
              'field' => 'slug', // using 'slug' but can be either 'term_id', 'slug' or 'name'
              'terms' => $term_arr // array of ID values, slugs or names depending what 'field' is set to
            )
          )
        );
        $related_posts = get_posts( $args );

        if($related_posts) {
                
          foreach ( $related_posts as $post ) : setup_postdata( $post ); 
            // output whatever you want here
            the_title(); // for example
          endforeach;

        } else {

          echo 'No Related Posts';

        }

    }

    wp_reset_postdata();
}
?>