Skip to content

Instantly share code, notes, and snippets.

@daveloodts
Created August 23, 2016 06:33
Show Gist options
  • Save daveloodts/7145ded04ca86a9fd4e2e21e28a451c2 to your computer and use it in GitHub Desktop.
Save daveloodts/7145ded04ca86a9fd4e2e21e28a451c2 to your computer and use it in GitHub Desktop.
Display latest posts of active category (except active post) / user in sidebar
<?php
// get the custom post type's taxonomy terms
$custom_taxterms = wp_get_object_terms( $post->ID, 'faq_category', array('fields' => 'ids') );
// arguments
$args = array(
'post_type' => 'qa_faqs',
'post_status' => 'publish',
'posts_per_page' => 10, // you may edit this number
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $custom_taxterms
)
),
'post__not_in' => array ($post->ID),
);
$related_items = new WP_Query( $args );
// loop over query
if ($related_items->have_posts()) :
echo '<ul class="relatedlist">';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
echo '</ul>';
endif;
// Reset Post Data
wp_reset_postdata();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment