WordPress.org

Make WordPress Core

Ticket #15016: wp_list_filter.2.diff

File wp_list_filter.2.diff, 2.8 KB (added by scribu, 4 years ago)

Use in WP_Query

  • wp-includes/functions.php

     
    30193019 * @return array A list of objects or object fields 
    30203020 */ 
    30213021function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) { 
    3022         if ( !is_array($list) ) 
    3023                 return array(); 
     3022        $list = wp_list_filter( $list, $args, $operator ); 
     3023        $list = wp_list_pluck( $list, $field ); 
    30243024 
    3025         if ( empty($args) ) 
    3026                 $args = array(); 
     3025        return $list; 
     3026} 
    30273027 
    3028         if ( empty($args) && !$field ) 
    3029                 return $list;   // nothing to do 
     3028/** 
     3029 * Filters a list of objects, based on a set of key => value arguments 
     3030 * 
     3031 * @since 3.1.0 
     3032 * 
     3033 * @param array $list An array of objects to filter 
     3034 * @param array $args An array of key => value arguments to match against each object 
     3035 * @param string $operator The logical operation to perform. 'or' means only one element 
     3036 *      from the array needs to match; 'and' means all elements must match. The default is 'and'. 
     3037 * @return array 
     3038 */ 
     3039function wp_list_filter( $list, $args = array(), $operator = 'and' ) { 
     3040        if ( empty( $args ) ) 
     3041                return $list; 
    30303042 
    3031         $count = count($args); 
     3043        $count = count( $args ); 
    30323044 
    30333045        $filtered = array(); 
    30343046 
    30353047        foreach ( $list as $key => $obj ) { 
    3036                 $matched = count( array_intersect_assoc( (array) ($obj), $args ) ); 
     3048                $matched = count( array_intersect_assoc( (array) $obj, $args ) ); 
    30373049                if ( ('and' == $operator && $matched == $count) || ('or' == $operator && $matched <= $count) ) { 
    3038                         if ( $field ) 
    3039                                 $filtered[] = $obj->$field; 
    3040                         else 
    3041                                 $filtered[$key] = $obj; 
     3050                        $filtered[$key] = $obj; 
    30423051                } 
    30433052        } 
    30443053 
     
    30463055} 
    30473056 
    30483057/** 
     3058 * Pluck a certain field out of each object in a list 
     3059 * 
     3060 * @since 3.1.0 
     3061 * 
     3062 * @param array $list A list of objects or arrays 
     3063 * @param int|string $field A field from the object to place instead of the entire object 
     3064 * @return array 
     3065 */ 
     3066function wp_list_pluck( $list, $field ) { 
     3067        foreach ( $list as $key => $value ) { 
     3068                $value = (array) $value; 
     3069                $list[ $key ] = $value[ $field ]; 
     3070        } 
     3071 
     3072        return $list; 
     3073} 
     3074 
     3075/** 
    30493076 * Determines if default embed handlers should be loaded. 
    30503077 * 
    30513078 * Checks to make sure that the embeds library hasn't already been loaded. If 
  • wp-includes/query.php

     
    19511951 
    19521952                        // Back-compat 
    19531953                        if ( !empty( $ids ) ) { 
    1954                                 $cat_query = wp_filter_object_list( $tax_query, array( 'taxonomy' => 'category' ) ); 
     1954                                $cat_query = wp_list_filter( $tax_query, array( 'taxonomy' => 'category' ) ); 
    19551955                                if ( !empty( $cat_query ) ) { 
    19561956                                        $cat_query = reset( $cat_query ); 
    19571957                                        $cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );