Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use display_post_states filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
April 20, 2023
5 minutes read

As a fellow WordPress developer, you know the importance of hooks in customizing your WordPress site without altering the core files. Today, let’s dive into the display_post_states filter. It filters the default post display states used in the posts list table. Using this hook, you can customize how post states are displayed in the admin panel.

Example 1: Always Display Current Post Status

This example demonstrates how you can always show the current post status in the labels.

    /**
     * This always shows the current post status in the labels.
     *
     * @param array   $states current states.
     * @param WP_Post $post current post object.
     * @return array
     */
    function weplugins_custom_display_post_states( $states, $post ) {
        /* Receive the post status object by post status name */
        $post_status_object = get_post_status_object( $post->post_status );
    }
    

Example 2: Modify Post States Based on Conditions

In this example, learn how to modify the $post_states variable according to your website requirements.

    function weplugins_modify_display_post_states_defaults($post_states, $post) { 
        // Update the $post_states variable according to your website requirements and return this variable. You can modify the $post_states variable conditionally too if you want.
        return $post_states; 
    }
    // add the filter
    add_filter( "display_post_states", "weplugins_modify_display_post_states_defaults", 10, 2 );
    

Example 3: Removing a Hook Callback

Here’s how you can remove a registered hook using remove_filter.

    remove_filter( "display_post_states", "weplugins_modify_display_post_states_defaults", 10, 2 );
    

Please ensure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

If you have any trouble using this hook or need further customization, please contact us at WePlugins and we’d be happy to assist you.

Access Premium WordPress Plugins

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.