Advertisement
Guest User

"All-in-One Event Calendar" filter_by_terms AND/OR

a guest
Feb 27th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.89 KB | None | 0 0
  1. function filter_by_terms( $post_ids, $term_ids )
  2. {
  3.         global $wpdb;
  4.        
  5.         // ===============================================
  6.         // = Sanitize provided IDs against SQL injection =
  7.         // ===============================================
  8.         if( ! is_array( $post_ids ) )
  9.             $post_ids = explode( ',', $post_ids );
  10.         foreach( $post_ids as &$post_id ) {
  11.             $post_id = intval( $post_id );
  12.         }
  13.         $post_ids = join( ',', $post_ids );
  14.        
  15.         if( ! is_array( $term_ids ) )
  16.             $term_ids = explode( ',', $term_ids );
  17.         foreach( $term_ids as &$term_id ) {
  18.             $term_id = intval( $term_id );
  19.         }
  20.        
  21.         $eventTaxonomy = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE taxonomy like 'events%'", OBJECT_K );
  22.        
  23.         foreach ($term_ids as $termID) {
  24.             $taxoIDs[$eventTaxonomy[$termID]->taxonomy][$termID] = $termID;
  25.         }
  26.        
  27.         $args['post_type'] = 'ai1ec_event';
  28.         $args['post_status'] = 'published';
  29.         $args['posts_per_page'] = 999;
  30.         $args['tax_query']['relation'] = 'AND';
  31.        
  32.         if ( !empty($taxoIDs['events_tags']) ) {
  33.             $args['tax_query'][0]['taxonomy'] = 'events_tags';
  34.             $args['tax_query'][0]['field'] = 'id';
  35.             $args['tax_query'][0]['terms'] = $taxoIDs['events_tags'];
  36.             $args['tax_query'][0]['operator'] = 'AND';
  37.         }
  38.        
  39.         if ( !empty($taxoIDs['events_categories']) ) {
  40.             $args['tax_query'][1]['taxonomy'] = 'events_categories';
  41.             $args['tax_query'][1]['field'] = 'id';
  42.             $args['tax_query'][1]['terms'] = $taxoIDs['events_categories'];
  43.             $args['tax_query'][1]['operator'] = 'IN';
  44.         }
  45.        
  46.         $events = new WP_Query( $args );
  47.        
  48.         return $wpdb->get_col( $events->request );
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement