Make WordPress Core

Ticket #11929: query-metadata.php

File query-metadata.php, 1.1 KB (added by wnorris, 15 years ago)
Line 
1<?php
2
3function get_objects_by_metadata($meta_type, $meta_key, $meta_value = '') {
4        if ( !$meta_type || !$meta_key )
5                return false;
6
7        if ( ! $table = _get_meta_table($meta_type) )
8                return false;
9
10        global $wpdb;
11
12        $type_column = esc_sql($meta_type . '_id');
13        $meta_key = stripslashes($meta_key);
14
15        $query = $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key );
16
17        if ( $meta_value )
18                $query .= $wpdb->prepare( " AND meta_value = %s", $meta_value );
19
20        $object_ids = $wpdb->get_col( $query );
21
22        $objects = array();
23
24        foreach ( (array) $object_ids as $id ) {
25                $objects[]= apply_filters("fill_object_$meta_type", false, $id);
26        }
27
28        return array_filter($objects);
29}
30
31
32function _fill_object_comment($comment, $id) {
33        return get_comment($id);
34}
35add_filter('fill_object_comment', '_fill_object_comment', 10, 2);
36
37
38function _fill_object_post($post, $id) {
39        return get_post($id);
40}
41add_filter('fill_object_post', '_fill_object_post', 10, 2);
42
43
44function _fill_object_user($user, $id) {
45        return get_userdata($id);
46}
47add_filter('fill_object_user', '_fill_object_user', 10, 2);
48
49?>