The main problem here is that $wp_query
is not an array. It’s and instance of WP_Query
class. That’s why it has some methods like have_posts
, the_post
, and so on…
On the other hand, your $related
is normal PHP array, so it doesn’t have any methods – so if you try to call any method, you’re getting fatal error.
Since the posts are already added to $related
array, the easies way to deal with them would be to just loop through that array:
if ( ! empty($related) ) {
foreach ( $related as $post) {
print_r($post);
// you can't use template tags in here unless you use setup_postdata function
}
}