In case you want to get custom posts by custom taxonomy in WordPress, it isn’t so hard like you think. But before we proceed, we need to have registered a custom post and a custom taxonomy. If those two are not registered then this is not the right post for you.

How to get related custom posts by custom taxonomy in Wordpress

The code below will get related custom posts from custom taxonomy in WordPress but won’t get related posts from general categories, related custom posts from general categories or related posts by tag. For the code to work, you will need to make some adjustments as indicated below the code snippet.

Paste this code where you want the filtered posts to appear in a page, probably somewhere in the single file template.

<?php  
$yourtaxonomy = wp_get_object_terms( $post->ID, 'your_custom_taxonomy', array('fields' => 'ids') );
$args = array(
'post_type' => 'custom_post_type',
'post_status' => 'publish',
'posts_per_page' => 4, 
'orderby' => 'rand',
'query_one' => array(
    array(
        'taxonomy' => 'your_custom_taxonomy',
        'field' => 'id',
        'terms' => $yourtaxonomy
    )
),
'post__not_in' => array ($post->ID),
);
$related_posts = new WP_Query( $args );
if ($related_posts->have_posts()) :
echo '<ul>';
while ( $related_posts->have_posts() ) : $related_posts->the_post();
?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();
?>

Make sure you:

1 – Change your_custom_taxonomy to your custom taxonomy
2 – Change custom_post_type to your custom post type
3 – Change the posts_per_page number to the number of posts you want to appear per page