Skip to content

Instantly share code, notes, and snippets.

@jbd91
Last active May 15, 2018 15:45
Show Gist options
  • Save jbd91/d26c8300edb4091ba4714674deb61fe7 to your computer and use it in GitHub Desktop.
Save jbd91/d26c8300edb4091ba4714674deb61fe7 to your computer and use it in GitHub Desktop.
WP Query - Related Posts - Custom Post Type
<?php
$custom_taxterms = wp_get_object_terms( $post->ID, 'related-service', array('fields' => 'ids') );
$args =
[
'post_type' => 'post',
'posts_per_page' => 5,
'post__not_in' => [$post->ID],
'tax_query' => [
[
'taxonomy' => 'related-service',
'field' => 'ID',
'terms' => $custom_taxterms,
],
],
];
$related = new WP_Query($args);
if ($related->have_posts()) : ?>
<?php while ($related->have_posts()) : $related->the_post(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment