Advertisement
Guest User

Untitled

a guest
Dec 18th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. function similar_query( $post_id , $taxonomy , $nr ){
  2. if( $nr > 0 ){
  3. $topics = wp_get_post_terms( $post_id , $taxonomy );
  4.  
  5. $terms = array();
  6. if( !empty( $topics ) ){
  7. foreach ( $topics as $topic ) {
  8. // $term = get_category( $topic );
  9. $term = $topic;
  10. array_push( $terms, $term -> slug );
  11. }
  12. }
  13.  
  14. if( !empty( $terms ) ){
  15. $query = new WP_Query( array(
  16. 'post__not_in' => array( $post_id ) ,
  17. 'posts_per_page' => $nr,
  18. 'orderby' => 'rand',
  19. 'post_type' => get_post_type($post_id),
  20. 'tax_query' => array(
  21. array(
  22. 'taxonomy' => $taxonomy ,
  23. 'field' => 'slug',
  24. 'terms' => $terms ,
  25. )
  26. )
  27. ));
  28. }else{
  29. $query = array();
  30. }
  31. }else{
  32. $query = array();
  33. }
  34.  
  35. return $query;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement