daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Sep 25th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.  * A demo function to compile a list of locations (WordPress Terms) based on a 'type' (also WordPress Terms: 'commercial', 'residential', 'land'). Requested and returned via AJAX.
  4.  *
  5.  * @author Phil Kershaw
  6.  */
  7. add_action('wp_ajax_get_locations', 'get_locations');
  8. add_action('wp_ajax_nopriv_get_locations', 'get_locations');
  9.  
  10. function get_locations()
  11. {
  12.     global $wpdb;
  13.     $type = $_POST['type'];
  14.  
  15.     $query = "  SELECT DISTINCT SQL_CALC_FOUND_ROWS wp_posts.ID
  16.                 FROM wp_posts
  17.                 INNER JOIN wp_term_relationships
  18.                   ON (wp_posts.ID = wp_term_relationships.object_id)
  19.                 INNER JOIN wp_term_taxonomy
  20.                   ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
  21.                 INNER JOIN wp_terms
  22.                   ON wp_term_taxonomy.term_id = wp_terms.term_id
  23.                 WHERE (wp_posts.post_type = 'sale')
  24.                   AND (wp_posts.post_status = 'publish')
  25.                   AND (wp_terms.slug = '$type')";
  26.  
  27.     $ids = $wpdb->get_results($wpdb->prepare($query));
  28.    
  29.     foreach($ids as $id)
  30.     {
  31.         $terms = wp_get_post_terms($id->ID, 'location');
  32.         foreach($terms as $term)
  33.         {
  34.             foreach($locations as $index => $location)
  35.             {
  36.                 $match = false;
  37.                 if ($location['slug'] == $term->slug)
  38.                 {
  39.                     $locations[$index]['total']++;
  40.                     $match = true;
  41.                     break;
  42.                 }
  43.             }
  44.             if ($match != true)
  45.             {
  46.                 $locations[] = array(
  47.                     'name' => $term->name,
  48.                     'slug' => $term->slug,
  49.                     'total' => 1
  50.                     );
  51.             }
  52.         }
  53.     }
  54.     sort($locations);
  55.     die(json_encode($locations));
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top