WordPress.org

Make WordPress Core

Ticket #18858: get.txt

File get.txt, 1.7 KB (added by sqlwiz, 3 years ago)

php source code for proposed function

Line 
1function get_post_type_objects_in_term( $term_ids, $post_types, $taxonomies, $args = array() ) {
2
3        global $wpdb;
4
5        if ( ! is_array( $term_ids ) )
6                $term_ids = array( $term_ids );
7
8        if ( ! is_array( $taxonomies ) )
9                $taxonomies = array( $taxonomies );
10
11        foreach ( (array) $taxonomies as $taxonomy ) {
12                if ( ! taxonomy_exists( $taxonomy ) )
13                        return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
14        }
15
16        if ( ! is_array( $post_types ) )
17                $post_types = array( $post_types );
18
19        foreach ( (array) $post_types as $post_type ) {
20                if ( ! post_type_exists( $post_type ) )
21                        return new WP_Error( 'invalid_post_type', __( 'Invalid Post Type' ) );
22        }
23
24        $defaults = array( 'order' => 'ASC' );
25        $args = wp_parse_args( $args, $defaults );
26        extract( $args, EXTR_SKIP );
27
28        $order = ( 'desc' == strtolower( $order ) ) ? 'DESC' : 'ASC';
29
30        $term_ids = array_map('intval', $term_ids );
31
32        $post_types = "'" . implode( "', '", $post_types ) . "'";
33        $taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
34        $term_ids = "'" . implode( "', '", $term_ids ) . "'";
35
36        $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->posts AS p INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id 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 ($term_ids) AND p.post_type IN ($post_types) ORDER BY tr.object_id $order");
37
38        if ( ! $object_ids )
39                return array();
40
41        return $object_ids;
42}