Advertisement
Guest User

filtering custom post types by all categories matching

a guest
Jul 30th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. //get the post's terms (troubleshooting - can be removed once figured out!
  2. $category_terms = wp_get_object_terms($post->ID, 'category');
  3. if(!empty($category_terms)){
  4. if(!is_wp_error( $category_terms )){
  5. echo 'Terms <ul>';
  6. foreach($category_terms as $term){
  7. echo '<li><a href="'.get_term_link($term->slug, 'category').'">'.$term->name.'</a></li>';
  8. }
  9. echo '</ul>';
  10. }
  11. }
  12. //get post terms done
  13. // get the custom post type's taxonomy terms
  14.  
  15. $custom_taxterms = wp_get_object_terms( $post->ID,
  16. 'category', array('fields' => 'ids') );
  17. // arguments
  18. $args = array(
  19. 'post_type' => 'program_listings',
  20. 'post_status' => 'publish',
  21. 'posts_per_page' => 5, // you may edit this number
  22. 'orderby' => 'rand',
  23. 'tax_query' => array(
  24. array(
  25. 'taxonomy' => 'category',
  26. 'field' => 'id',
  27. 'terms' => $custom_taxterms
  28. )
  29. ),
  30. 'post__not_in' => array ($post->ID),
  31. );
  32. $related_items = new WP_Query( $args );
  33. // loop over query
  34. if ($related_items->have_posts()) :
  35. echo '<h2>Other Programs in this category</h2><ul>';
  36. while ( $related_items->have_posts() ) : $related_items->the_post();
  37. ?>
  38. <li style="margin-left:10px;list-style:none;"><a href="<?php the_permalink(); ?>"
  39. title="<?php the_title_attribute(); ?>">
  40. <?php the_title(); ?></a></li>
  41. <?php
  42. endwhile;
  43. echo '</ul>';
  44. endif;
  45. // Reset Post Data
  46. wp_reset_postdata();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement