Skip to content

Admin filters

John Blackbourn edited this page Aug 13, 2019 · 12 revisions

Extended CPTs provides several controls that can be added to the top of the post type listing screen so your editors can filter the screen by various fields. These controls live next to the default date and category dropdowns which WordPress provides.

Admin filters are specified with the admin_filters parameter.

Example

register_extended_post_type( 'article', array(

	'admin_filters' => array(
		'foo' => array(
			'title'    => 'Foo',
			'meta_key' => 'foo',
		),
		'bar' => array(
			'title'           => 'Bar',
			'meta_search_key' => 'bar',
		),
		'genre' => array(
			'title'    => 'Genre',
			'taxonomy' => 'genre',
		),
	),

) );

Available filter types

Post Meta Field Dropdown

Parameter: meta_key

Displays a select dropdown populated with all the existing values for the given meta key:

'foo' => array(
	'title'    => 'Foo',
	'meta_key' => 'foo',
),

You can also manually specify a list of values if you wish, either as a list or as a callback function:

'foo' => array(
	'title'    => 'Foo',
	'meta_key' => 'foo',
	'options'  => array(
		'one'   => 'One',
		'two'   => 'Two',
		'three' => 'Three',
	),
),
'foo' => array(
	'title'    => 'Foo',
	'meta_key' => 'foo',
	'options'  => 'get_foo_values',
),

Post Meta Field Search Box

Parameter: meta_search_key

Displays a text input for searching for posts that have that value for the given meta key. Uses a LIKE '%{value}%' query in SQL.

'foo' => array(
	'title'           => 'Foo',
	'meta_search_key' => 'foo',
),

Post Meta Field Exists Checkbox/Dropdown

Parameter: meta_exists

Displays a checkbox or a select dropdown for filtering by posts which have a meta field with the given key and a non-empty value. This performs a NOT IN ( '', '0', 'false', 'null' ) query for the meta value.

If just one value is passed as the meta_exists parameter, a checkbox will be shown:

'help_needed' => array(
	'meta_exists' => array(
		'complete' => 'Complete',
	),
),

If multiple values are passed, a select dropdown will be shown for the user to choose from:

'review_type' => array(
	'title'       => 'Review Type',
	'meta_exists' => array(
		'complete' => 'Complete',
		'broken'   => 'Broken',
	),
),

Post Meta Key Exists Checkbox/Dropdown

Parameter: meta_key_exists

Displays a checkbox or a select dropdown for filtering by posts which have a meta field with the given key, regardless of its value. This performs an EXISTS query for the meta key.

If just one value is passed as the meta_key_exists parameter, a checkbox will be shown:

'help_needed' => array(
	'meta_key_exists' => array(
		'help_needed' => 'Help Needed',
	),
),

If multiple values are passed, a select dropdown will be shown for the user to choose from:

'review_type' => array(
	'title'       => 'Review Type',
	'meta_key_exists' => array(
		'editors_choice' => 'Editors choice',
		'help_needed'    => 'Help Needed',
	),
),

Post Author Dropdown

Parameter: post_author

Displays a select dropdown of all authors of this post type:

'author' => [
	'post_author' => true,
],

You can also manually specify a list of user IDs if you wish, either as a list or as a callback function:

'author' => array(
	'post_author' => true,
	'options'     => array( 1, 7, 35, 220 ),
	),
),
'author' => array(
	'post_author' => true,
	'options'     => 'get_author_values',
),

Post Date Picker

Parameter: post_date

Displays a date picker (using the browser's native date input type). Typically used to provide From and To date range selection functionality:

'post_after' => [
	'post_date'  => 'after',
	'date_query' => [
		'column' => 'post_modified_gmt',
	],
],
'post_before' => [
	'post_date'  => 'before',
	'date_query' => [
		'column' => 'post_modified_gmt',
	],
],

The value of the post_date parameter is the name of the field to use in the date_query portion of the query and probably doesn't work well with anything other than after and before.

The optional date_query parameter can be used to pass additional parameters to the date query.

Taxonomy Terms Dropdown

Parameter: taxonomy

Displays a select dropdown populated with all the available terms for the given taxonomy:

'genre' => array(
	'title'    => 'Genre',
	'taxonomy' => 'genre',
),

Restricting Visibility by User Capability

Parameter: cap

Any filter can be restricted so it's only shown to users with a given capability by using the cap parameter:

'my_filter' => array(
	'title'    => 'Admin-Only Filter',
	'meta_key' => 'foo',
	'cap'      => 'manage_options',
),

Optional Parameters

The title parameter is optional for any filter. If omitted, a title will be generated based on the meta key or taxonomy name. The title is used to label the field as appropriate.

The default parameter can be used to specify a default value for the filter, which means the post list will be filtered by that value by default.

The meta_query parameter can be used to pass an array of arguments to the WP_Meta_Query that's generated when filtering the screen by meta fields.

The label parameter is optional and will be generated based on the filter type if omitted. This is used for the <label> that's only shown to screen readers for certain filter types.