category__and, tag__and, tag_slug__in, tag_slug__and support. fixes #16157

git-svn-id: http://svn.automattic.com/wordpress/trunk@17244 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2011-01-09 16:19:48 +00:00
parent 6baed7c3ea
commit f06af8506b
2 changed files with 63 additions and 8 deletions

View File

@ -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'
);
}

View File

@ -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++;