trying to display random posts from each category that my custom post belongs to (it is related to 2 categories). I am trying to get post categories by id. I have tried to put category ID manually but still it display random posts from ALL posts in his type. Can anyone help me with this?
<?php
$term_list = wp_get_post_terms( $post->ID, 'listing-category', array( 'fields' => 'ids' ) );
$args = array(
'post_type' => 'listing',
'category' => $term_list[0],
'post_status' => 'publish',
'orderby' => 'rand',
'posts_per_page' => 3,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$rand_posts .= '<li> <a href="'. get_the_permalink() .'">'. get_the_title() .'</a> </li>';
}
echo $rand_posts;
wp_reset_postdata();
}
?>