From f59c869e6215341db7b925c80e99a3158d0dc928 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Sun, 9 Jan 2011 16:19:48 +0000 Subject: [PATCH] category__and, tag__and, tag_slug__in, tag_slug__and support. fixes #16157 git-svn-id: https://develop.svn.wordpress.org/trunk@17244 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/query.php | 53 +++++++++++++++++++++++++++++++++++----- wp-includes/taxonomy.php | 18 ++++++++++++-- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index c250e05603..5e29416619 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -1692,7 +1692,7 @@ class WP_Query { } if ( !empty($q['category__in']) ) { - $q['category__in'] = array_unique( $q['category__in'] ); + $q['category__in'] = array_map('absint', array_unique( $q['category__in'] ) ); $tax_query[] = array( 'taxonomy' => 'category', 'terms' => $q['category__in'], @@ -1701,34 +1701,75 @@ class WP_Query { } if ( !empty($q['category__not_in']) ) { - $q['category__not_in'] = array_unique( $q['category__not_in'] ); + $q['category__not_in'] = array_map('absint', array_unique( $q['category__not_in'] ) ); $tax_query[] = array( 'taxonomy' => 'category', 'terms' => $q['category__not_in'], - 'operator' => 'NOT IN', + 'operator' => 'NOT IN' + ); + } + + if ( !empty($q['category__and']) ) { + $q['category__and'] = array_map('absint', array_unique( $q['category__and'] ) ); + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $q['category__and'], + 'field' => 'term_id', + 'operator' => 'AND' ); } // Tag stuff if ( !empty($q['tag_id']) ) { + $q['tag_id'] = absint( $q['tag_id'] ); $tax_query[] = array( 'taxonomy' => 'post_tag', - 'terms' => $q['tag_id'], + 'terms' => $q['tag_id'] ); } if ( !empty($q['tag__in']) ) { + $q['tag__in'] = array_map('absint', array_unique( $q['tag__in'] ) ); $tax_query[] = array( 'taxonomy' => 'post_tag', - 'terms' => $q['tag__in'], + 'terms' => $q['tag__in'] ); } if ( !empty($q['tag__not_in']) ) { + $q['tag__not_in'] = array_map('absint', array_unique( $q['tag__not_in'] ) ); $tax_query[] = array( 'taxonomy' => 'post_tag', 'terms' => $q['tag__not_in'], - 'operator' => 'NOT IN', + 'operator' => 'NOT IN' + ); + } + + if ( !empty($q['tag__and']) ) { + $q['tag__and'] = array_map('absint', array_unique( $q['tag__and'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag__and'], + 'operator' => 'AND' + ); + } + + if ( !empty($q['tag_slug__in']) ) { + $q['tag_slug__in'] = array_map('sanitize_title', $q['tag_slug__in']); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag_slug__in'], + 'field' => 'slug' + ); + } + + if ( !empty($q['tag_slug__and']) ) { + $q['tag_slug__and'] = array_map('sanitize_title', $q['tag_slug__and']); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag_slug__and'], + 'field' => 'slug', + 'operator' => 'AND' ); } diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index ebf6e9c418..c4c9776229 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -671,8 +671,7 @@ class WP_Tax_Query { $join .= " ON ($primary_table.$primary_id_column = $alias.object_id)"; $where[] = "$alias.term_taxonomy_id $operator ($terms)"; - } - elseif ( 'NOT IN' == $operator ) { + } elseif ( 'NOT IN' == $operator ) { if ( empty( $terms ) ) continue; @@ -684,6 +683,21 @@ class WP_Tax_Query { FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($terms) )"; + } elseif ( 'AND' == $operator ) { + + if ( empty( $terms ) ) + continue; + + $num_terms = count( $terms ); + + $terms = implode( ',', $terms ); + + $where[] = "$primary_table.$primary_id_column IN ( + SELECT object_id + FROM $wpdb->term_relationships + WHERE term_taxonomy_id IN ($terms) + GROUP BY object_id HAVING COUNT(object_id) = $num_terms + )"; } $i++;