Advertisement
sscovil

Random list of related WP posts based on taxonomy

Dec 31st, 2012
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. <?php
  2. /*
  3. * For WP support forum question at:
  4. * http://wordpress.org/support/topic/multiple-taxonomy-with-next_post_link
  5. */
  6.  
  7. $produit_type = array();
  8. $types = get_terms( 'produit_type' );
  9. foreach ( $types as $type ) {
  10.     $produit_type[] = $term->slug;
  11. }
  12.  
  13. $produit_marque = array();
  14. $types = get_terms( 'produit_marque' );
  15. foreach ( $types as $type ) {
  16.     $produit_marque[] = $term->slug;
  17. }
  18.  
  19. global $post;
  20. $current_post[] = $post->ID;
  21.  
  22. $args = array(
  23.     'post__not_in' => $current_post, // don't include current post
  24.     'orderby' => 'rand', // randomize list
  25.     'tax_query' => array(
  26.         'relation' => 'AND',
  27.         array(
  28.             'taxonomy' => 'produit_type',
  29.             'field' => 'slug',
  30.             'terms' => $produit_type, // array of taxonomy terms of current post
  31.             'operator' => 'IN',
  32.         ),
  33.         array(
  34.             'taxonomy' => 'produit_marque',
  35.             'field' => 'slug',
  36.             'terms' => $produit_marque, // array of taxonomy terms of current post
  37.             'operator' => 'IN',
  38.         ),
  39.     ),
  40. );
  41.  
  42. $related = new WP_Query( $args );
  43. echo '<ul>';
  44.  
  45. // begin the loop
  46. while ( $related->have_posts() ) : $related->the_post();
  47.     echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
  48. endwhile; // end the loop
  49.  
  50. echo '</ul>';
  51. wp_reset_postdata();
  52.  
  53. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement