In the function wp_get_post_terms()
you can select the taxonomy. the default is post_tag
you can change it to category
.
And in the WP_Query
args you need to change the tag__in
to category__in
.
// get the categories
$categories = wp_get_post_terms($post->ID, 'category');
if ($categories) {
$categories_ids = array();
foreach($categories as $individual_cat) $categories_ids[] = $individual_cat->term_id;
$args=array(
'category__in' => $categories_ids, // Select posts from this categories
'post__not_in' => array($post->ID),
'posts_per_page'=>10, // Number of related posts to display.
'ignore_sticky_posts'=>1
);
$my_query = new wp_query( $args );
$num=1;
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<li>
<a href="https://wordpress.stackexchange.com/questions/291639/<?php the_permalink(); ?>">
...
</a>
</li>
<?php
}
wp_reset_postdata();
}