Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! function_exists('search_and_go_elated_get_related_listings')) {
  4.  
  5. function search_and_go_elated_get_related_listings( $options = array() ) {
  6.  
  7. $listing_id = get_the_ID();
  8. $category_tax = 'listing-item-category';
  9. $tag_tax = 'listing-item-tag';
  10.  
  11. $categories = wp_get_object_terms( $listing_id, $category_tax );
  12. $tags = wp_get_object_terms( $listing_id, $tag_tax );
  13. //Get listing type and features list
  14. $listing_type_id = get_post_meta( $listing_id, 'eltd_listing_item_type', true );
  15. $listing_type_feature_list = get_post_meta($listing_type_id, 'eltd_listing_type_feature_list', true);
  16.  
  17. $cat_ids = array();
  18. $tag_ids = array();
  19.  
  20. //Get all category ids
  21. if ( $categories ) {
  22. foreach ( $categories as $category ) {
  23. $cat_ids[] = $category->term_id;
  24. }
  25. }
  26.  
  27. //Get all tag ids
  28. if ( $tags ) {
  29. foreach ( $tags as $tag ) {
  30. $tag_ids[] = $tag->term_id;
  31. }
  32. }
  33.  
  34. //Try to find related listings by category
  35. $related_listings = search_and_go_elated_related_listing_filter_by_taxonomy( $listing_id, $listing_type_id, $cat_ids, $category_tax, $listing_type_feature_list, $options );
  36. $has_related_by_category = false;
  37. if ( $related_listings->have_posts() ) {
  38. $has_related_by_category = true;
  39. }
  40.  
  41. //If search by category fails try to search by tags
  42. if ( ! $has_related_by_category ) {
  43. $related_listings = search_and_go_elated_related_listing_filter_by_taxonomy( $listing_id, $listing_type_id, $tag_ids, $tag_tax, $listing_type_feature_list, $options );
  44. }
  45.  
  46. return $related_listings;
  47.  
  48. }
  49.  
  50. }
  51.  
  52. if ( ! function_exists( 'search_and_go_elated_related_listing_filter_by_taxonomy' ) ) {
  53.  
  54. function search_and_go_elated_related_listing_filter_by_taxonomy( $listing_id, $listing_type_id, $tax_ids, $taxonomy, $feature_list = array(), $options ) {
  55.  
  56. //Query options
  57. $posts_per_page = -1;
  58.  
  59. //Override query options
  60. extract($options);
  61.  
  62. //Get all features and create meta query from checked features
  63. $meta_query = array();
  64. $meta_query_feature_list = array();
  65. if ( is_array( $feature_list ) && count( $feature_list ) ) {
  66. foreach ( $feature_list as $feature_item ) {
  67. if ( get_post_meta( $listing_id, 'listing_feature_list_'.$listing_type_id.'_'.sanitize_title($feature_item), true) == 1) {
  68. $meta_query_feature_list[] = array(
  69. 'key' => 'listing_feature_list_'.$listing_type_id.'_'.sanitize_title($feature_item),
  70. 'value' => '1'
  71. );
  72. }
  73. }
  74. $meta_query = array(
  75. 'relation' => 'AND',
  76. array(
  77. 'key' => 'eltd_listing_item_type',
  78. 'value' => $listing_type_id
  79. ),
  80. // array(
  81. // 'relation' => 'OR',
  82. // $meta_query_feature_list
  83. // )
  84. );
  85. }
  86.  
  87. $args = array(
  88. 'post_type' => 'listing-item',
  89. 'post__not_in' => array($listing_id),
  90. 'order' => 'DESC',
  91. 'orderby' => 'date',
  92. 'posts_per_page'=> $posts_per_page,
  93. 'tax_query' => array(
  94. array(
  95. 'taxonomy' => $taxonomy,
  96. 'field' => 'term_id',
  97. 'terms' => $tax_ids,
  98. ),
  99. ),
  100. 'meta_query' => $meta_query
  101. );
  102.  
  103. $query = new WP_Query( $args );
  104.  
  105. return $query;
  106.  
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement