How can I get the most Related posts by taxonomy?

In ‘genre’ taxonomy, there are action, comedy, sci-fi …

I want to get related posts and save it into ‘movie’ ordered by most matched taxonomy.

Followings are my codes But it didn’t work…

$custom_taxterms = wp_get_object_terms($post->ID, ‘genre’, array(
‘fields’ => ‘ids’
));

if ($custom_taxterms = wp_get_object_terms($post->ID, ‘genre’, array(
‘fields’ => ‘ids’
))) {
$args = array(
‘post_type’ => ‘show’,
‘posts_per_page’ => 5,
‘post__not_in’ => array(
$post->ID
) ,
‘paged’ => $paged,
‘tax_query’ => array(
‘relation’ => ‘OR’
) ,
‘orderby’ => ‘asc’,
);
foreach($custom_taxterms as $custom_taxterm) {
$arg[‘tax_query’][] = array(
‘taxonomy’ => ‘genre’,
‘field’ => ‘id’,
‘terms’ => $custom_taxterm->term_id,
‘operator’ => ‘IN’,
);
}
}

$movie = new WP_Query($arg);

Read more here:: How can I get the most Related posts by taxonomy?

Leave a Reply

Your email address will not be published. Required fields are marked *