Skip to content

Admin filters

John Blackbourn edited this page Jun 4, 2015 · 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

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

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

Displays a checkbox or a select dropdown for filtering by posts which have a meta field with the given key, regardless of its value (more specifically, if its value isn't empty-like, such as 0 or false).

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

'help_needed' => array(
	'meta_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_exists' => array(
		'editors_choice' => 'Editors choice',
		'help_needed'    => 'Help Needed',
	),
),

Taxonomy Terms Dropdown

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

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

Note that this filter type requires the Extended Taxonomies library to be present.

Restricting Visibility by User Capability

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 actually optional for any filter. If omitted, a title will be generated based on the meta key, taxonomy name, etc.

Clone this wiki locally