display_post_statesfilter-hookWP 2.8.0

Allows you to add a marker (state or label) to a post in the admin Posts table.

Such state labels can be seen, for example, when a post is a draft, pending review, and so on.

This hook can add custom markers to any post type in the admin posts table: posts, pages, and custom post types.

Usage

add_filter( 'display_post_states', 'wp_kama_display_post_states_filter', 10, 2 );

/**
 * Function for `display_post_states` filter-hook.
 * 
 * @param array $post_states A mapping of post state slugs to translated post state labels. E.g. `array( 'draft' => __( 'Draft' ), 'sticky' => __( 'Sticky' ), ... )`.
 * @param WP_Post               $post        The current post object.
 *
 * @return array
 */
function wp_kama_display_post_states_filter( $post_states, $post ){

	// filter...
	return $post_states;
}
$post_states(array)
Array of post state labels.
$post(WP_Post)
Current post object whose states are being filtered.

Examples

#1 Add custom post state labels to the Pages table

The labels are added to posts of the page post type.

## Labels for special pages in the Pages table.
add_filter( 'display_post_states', 'special_page_mark', 10, 2 );
function special_page_mark( $post_states, $post ){
	if( $post->post_type === 'page' ){
		if( $post->post_name === 'my-account' ){
			$post_states[] = 'My Account page';
		}

		if( in_array( $post->post_name, ['cart','shop','checkout'] ) ){
			$post_states[] = 'Special page';
		}
	}

	return $post_states;
}

The result:

#2 Remove the "Classic Editor" label

After the Classic Editor plugin is installed, posts receive a label indicating which editor was used. This can make the table harder to read:

It is easy to remove this or any other label if its key is known:

add_filter( 'display_post_states', 'remove_classic_editor_mark', 10, 2 );

function remove_classic_editor_mark( $post_states, $post ) {
	unset( $post_states['classic-editor-plugin'] ); // "Classic Editor" or "Block Editor".

	// Post statuses.
	unset( $post_states['protected'] ); // Password protected.
	unset( $post_states['private'] );   // Private.
	unset( $post_states['draft'] );     // Draft.
	unset( $post_states['pending'] );   // Pending review.
	unset( $post_states['sticky'] );    // Sticky.
	unset( $post_states['scheduled'] ); // Scheduled.

	// Special labels.
	unset( $post_states['page_on_front'] );          // Front page.
	unset( $post_states['page_for_posts'] );         // Posts page.
	unset( $post_states['page_for_privacy_policy'] ); // Privacy Policy page.

	return $post_states;
}

Changelog

Since 2.8.0 Introduced.
Since 3.6.0 Added the $post parameter.
Since 5.5.0 Also applied in the Customizer context. If any admin functions are used within the filter, their existence should be checked with function_exists() before being used.

Where the hook is called

get_post_states()
display_post_states
wp-admin/includes/template.php 2366
return apply_filters( 'display_post_states', $post_states, $post );

Where the hook is used in WordPress

Usage not found.