From 13a01467dcce65aa93b8a7e463553238c154dbbe Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 29 May 2007 04:25:09 +0000 Subject: [PATCH] get_objects_in_term() git-svn-id: http://svn.automattic.com/wordpress/trunk@5585 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/query.php | 2 +- wp-includes/taxonomy.php | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index 14d8c8aef6..413ff7d062 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -859,7 +859,7 @@ class WP_Query { if ( strlen($in_cats) > 0 ) $in_cats = " AND $wpdb->term_taxonomy.term_id IN ({$q['cat']}) "; if ( strlen($out_cats) > 0 ) { - // TODO + // TODO use get_objects_in_term $ids = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE $wpdb->post2cat.category_id IN ($out_cats)"); if ( is_array($ids) && count($ids > 0) ) { foreach ( $ids as $id ) diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index fac44766f8..35d1943467 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -366,6 +366,30 @@ function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) { return $tt_ids; } +function get_objects_in_term( $terms, $taxonomies, $args = array() ) { + global $wpdb; + + if ( !is_array( $terms) ) + $terms = array($terms); + + if ( !is_array($taxonomies) ) + $taxonomies = array($taxonomies); + + $defaults = array('order' => 'ASC'); + $args = wp_parse_args( $args, $defaults ); + extract($args); + + $taxonomies = "'" . implode("', '", $taxonomies) . "'"; + $terms = "'" . implode("', '", $terms) . "'"; + + $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($in_terms) ORDER BY tr.object_id $order"); + + if ( ! $object_ids ) + return array(); + + return $object_ids; +} + /** * Returns the terms associated with the given object(s), in the supplied taxonomies. * @param int|array $object_id The id of the object(s)) to retrieve for.