WordPress: Filtering Posts by Meta Data in WordPress Admin

Organising and finding content in WordPress admin can be beneficial, especially when you have many posts (or other custom post types) with different properties. Using custom coding, you can extend wp-admin with filtering options that make it easier to find and sort content based on their specific metadata.

In my example, I focus on filtering by 'car type' for the post type 'car'. You can use the same approach to filter by other metadata types, such as tags, categories, dates and more.

/* Create filter function for parents / variants */
add_action( 'pre_get_posts', 'FilterParentAndVariants');
function FilterParentAndVariants( $query ){
	global $pagenow;
	$pt = 'bil';
	if ( $pagenow=='edit.php' && isset($_GET['CarType']) && (isset($_GET['post_type']))) && $_GET['post_type'] == $pt) {
		$query->set('meta_key', 'type');
		$query->set('meta_value', $_GET['CarType']);

add_action('restrict_manage_posts', 'filterPostsByCarType',9999);
function filterPostsByCarType(){
	global $pagenow;
	$pt = 'car';
	if ((isset($_GET['post_type']))) && $_GET['post_type'] == $pt && is_admin() && $pagenow=='edit.php') {
		$GETvalue = '&CarType=';
		$SelectOptions = ''.__('Filter Parent/variants').'';
			$optionValues = ['parent', 'variant'];
			foreach ( $optionValues as $optionValue ) {
				$SelectOptions .= ''.$optionValue.'';
		$SelectOptions .= '';
		echo $SelectOptions;

pre_get_posts" is a WordPress action that allows to modify and customise the default WP_Query query before it is sent to the database. In my code snippet, I use pre_get_posts to add a filter function in the admin part that allows to filter posts by a specific metadata type if the exact GET parameter is set.

I use "restrict_manage_posts" to add a dropdown menu above the posts list in the admin interface. This dropdown menu allows you to filter the posts based on a specific metadata type (in my example, "car type") and post type "car". In the array for $optionsvalues, enter the options you want to be able to filter on. In my example, it's just "parent" and "variant". If you have many, it might be a good idea to make it dynamic so that it retrieves the options based on the selected meta key.

This code example should be incorporated into either your theme's "functions.php" file or via a plugin. Please note that this is a customised solution and it's always a good idea to make a backup before making any changes to the code if you don't feel confident.