Title: WP_List_Util::filter
Published: December 7, 2016
Last modified: August 20, 2026

---

# WP_List_Util::filter( array $args = array(), string $operator = ‘AND’ ): array

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_list_util/filter/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_list_util/filter/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_list_util/filter/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_list_util/filter/?output_format=md#source)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_list_util/filter/?output_format=md#changelog)

Filters the list, based on a set of key => value arguments.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_list_util/filter/?output_format=md#description)󠁿

Retrieves the objects from the list that match the given arguments.
Key represents
property name, and value represents property value.

If an object has more properties than those specified in arguments, that will not
disqualify it. When using the ‘AND’ operator, any missing properties will disqualify
it.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_list_util/filter/?output_format=md#parameters)󠁿

 `$args`arrayoptional

An array of key => value arguments to match against each object.

Default:`array()`

`$operator`stringoptional

The logical operation to perform. `'AND'` means all elements from the array must
match. `'OR'` means only one element needs to match. `'NOT'` means no elements may
match. Default `'AND'`.

Default:`'AND'`

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_list_util/filter/?output_format=md#return)󠁿

 array Array of found values.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_list_util/filter/?output_format=md#source)󠁿

    ```php
    public function filter( $args = array(), $operator = 'AND' ) {
    	if ( empty( $args ) ) {
    		return $this->output;
    	}

    	$operator = strtoupper( $operator );

    	if ( ! in_array( $operator, array( 'AND', 'OR', 'NOT' ), true ) ) {
    		$this->output = array();
    		return $this->output;
    	}

    	$count    = count( $args );
    	$filtered = array();

    	foreach ( $this->output as $key => $obj ) {
    		$matched = 0;

    		foreach ( $args as $m_key => $m_value ) {
    			if ( is_array( $obj ) ) {
    				// Treat object as an array.
    				if ( array_key_exists( $m_key, $obj ) && ( $m_value == $obj[ $m_key ] ) ) {
    					++$matched;
    				}
    			} elseif ( is_object( $obj ) ) {
    				// Treat object as an object.
    				if ( isset( $obj->{$m_key} ) && ( $m_value == $obj->{$m_key} ) ) {
    					++$matched;
    				}
    			}
    		}

    		if ( ( 'AND' === $operator && $matched === $count )
    			|| ( 'OR' === $operator && $matched > 0 )
    			|| ( 'NOT' === $operator && 0 === $matched )
    		) {
    			$filtered[ $key ] = $obj;
    		}
    	}

    	$this->output = $filtered;

    	return $this->output;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-list-util.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wp-list-util.php#L98)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp-list-util.php#L98-L141)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_list_util/filter/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.7.0](https://developer.wordpress.org/reference/since/4.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_list_util%2Ffilter%2F)
before being able to contribute a note or feedback.