Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! function_exists('libero_mikado_get_related_post_type')) {
  4. /**
  5. * Function for returning latest posts types
  6. *
  7. * @param $post_id
  8. * @param array $options
  9. * @return WP_Query
  10. */
  11. function libero_mikado_get_related_post_type($post_id, $options = array()) {
  12.  
  13. $post_type = get_post_type($post_id);
  14.  
  15. //Get tags
  16. $tags = ($post_type == 'portfolio-item') ? wp_get_object_terms($post_id, 'portfolio-tag') : get_the_tags($post_id);
  17. //Get categories
  18. $categories = ($post_type == 'portfolio-item') ? wp_get_object_terms($post_id, 'portfolio-category') : get_the_category($post_id);
  19.  
  20. $tag_ids = array();
  21. if ($tags) {
  22. foreach ($tags as $tag) {
  23. $tag_ids[] = $tag->term_id;
  24. }
  25. }
  26.  
  27. $category_ids = array();
  28. if ($categories) {
  29. foreach ($categories as $category) {
  30. $category_ids[] = $category->term_id;
  31. }
  32. }
  33.  
  34. $hasRelatedByTag = false;
  35. $hasRelatedByCategory = false;
  36.  
  37. if ($tag_ids) {
  38.  
  39. if ($post_type == 'portfolio-item') {
  40. $related_by_tag = libero_mikado_get_related_custom_post_type_by_param($post_id, $tag_ids, 'portfolio-tag', $options); //For Custom Posts
  41. } else {
  42. $related_by_tag = libero_mikado_get_related_posts($post_id, $tag_ids, 'tag', $options);
  43. }
  44.  
  45. if (!empty($related_by_tag->posts)) {
  46. $hasRelatedByTag = true;
  47. return $related_by_tag;
  48. }
  49.  
  50. $hasRelatedByTag = false;
  51. }
  52.  
  53. if ($categories && !$hasRelatedByTag) {
  54.  
  55. if ($post_type == 'portfolio-item') {
  56. $related_by_category = libero_mikado_get_related_custom_post_type_by_param($post_id, $category_ids, 'portfolio-category', $options);
  57. } else {
  58. $related_by_category = libero_mikado_get_related_posts($post_id, $category_ids, 'category', $options);
  59. }
  60.  
  61. if (!empty($related_by_category->posts)) {
  62. $hasRelatedByCategory = true;
  63. return $related_by_category;
  64. }
  65.  
  66. $hasRelatedByCategory = false;
  67. }
  68. }
  69. }
  70.  
  71. if ( ! function_exists('libero_mikado_get_related_posts') ) {
  72. /**
  73. * Function for related posts
  74. *
  75. * @param $post_id - Post ID
  76. * @param $term_ids - Category or Tag IDs
  77. * @param $slug - term slug for WP_Query
  78. * @param array $options
  79. * @return WP_Query
  80. */
  81. function libero_mikado_get_related_posts($post_id, $term_ids, $slug, $options = array()) {
  82.  
  83. //Query options
  84. $posts_per_page = -1;
  85.  
  86. //Override query options
  87. extract($options);
  88.  
  89. $args = array(
  90. 'post__not_in' => array($post_id),
  91. $slug . '__in' => $term_ids,
  92. 'order' => 'DESC',
  93. 'orderby' => 'date',
  94. 'posts_per_page' => $posts_per_page
  95. );
  96.  
  97. $related_posts = new WP_Query($args);
  98. wp_reset_postdata();
  99.  
  100. return $related_posts;
  101. }
  102. }
  103.  
  104. if ( ! function_exists('libero_mikado_get_related_custom_post_type_by_param') ) {
  105. /**
  106. * @param $post_id - Post ID
  107. * @param $term_ids - Category or Tag IDs
  108. * @param $taxonomy
  109. * @param array $options
  110. * @return WP_Query
  111. */
  112. function libero_mikado_get_related_custom_post_type_by_param($post_id, $term_ids, $taxonomy, $options = array()) {
  113.  
  114. //Query options
  115. $posts_per_page = -1;
  116.  
  117. //Override query options
  118. extract($options);
  119.  
  120. $args = array(
  121. 'post__not_in' => array($post_id),
  122. 'order' => 'DESC',
  123. 'orderby' => 'date',
  124. 'posts_per_page'=> $posts_per_page,
  125. 'tax_query' => array(
  126. array(
  127. 'taxonomy' => $taxonomy,
  128. 'field' => 'term_id',
  129. 'terms' => $term_ids,
  130. ),
  131. )
  132. );
  133.  
  134. $related_by_taxonomy = new WP_Query($args);
  135.  
  136. return $related_by_taxonomy;
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement