Advertisement
salmancreation

wordpress cat relative post query

Oct 3rd, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1.  <?php $orig_post = $post;
  2. global $post;
  3. $categories = get_the_category($post->ID);
  4. if ($categories) {
  5. $category_ids = array();
  6. foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
  7. $args=array(
  8. 'category__in' => $category_ids,
  9. 'post__not_in' => array($post->ID),
  10. 'posts_per_page'=> 5, // Number of related posts that will be displayed.
  11. // 'caller_get_posts'=>1,
  12. // 'orderby'=>'rand' // Randomize the posts
  13. );
  14.  
  15. $my_query = new wp_query( $args );
  16. if( $my_query->have_posts() ) {
  17. while( $my_query->have_posts() ) {
  18. $my_query->the_post(); ?>
  19.  
  20. <?php the_title(); ?> // for checking
  21. <hr> // for checking
  22.  
  23. <? }
  24. } }
  25.  
  26. $post = $orig_post;
  27. wp_reset_query(); ?>
  28.  
  29. Please use this query for related post (I mean second query on your code)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement