Last active
May 15, 2018 15:45
-
-
Save jbd91/d26c8300edb4091ba4714674deb61fe7 to your computer and use it in GitHub Desktop.
WP Query - Related Posts - Custom Post Type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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