Creating/editing custom ‘post-state’

_post_states( WP_Post $post )

Parameters #Parameters

$post
(WP_Post) (Required)

Code Reference from wordpress.org (https://developer.wordpress.org/reference/functions/_post_states/)

function _post_states($post) {
$post_states = array();
if ( isset( $_REQUEST['post_status'] ) )
    $post_status = $_REQUEST['post_status'];
else
    $post_status="";

if ( !empty($post->post_password) )
    $post_states['protected'] = __('Password protected');
if ( 'private' == $post->post_status && 'private' != $post_status )
    $post_states['private'] = __('Private');
if ( 'draft' === $post->post_status ) {
    if ( get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
        $post_states[] = __( 'Customization Draft' );
    } elseif ( 'draft' !== $post_status ) {
        $post_states['draft'] = __( 'Draft' );
    }
} elseif ( 'trash' === $post->post_status && get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
    $post_states[] = __( 'Customization Draft' );
}
if ( 'pending' == $post->post_status && 'pending' != $post_status )
    $post_states['pending'] = _x('Pending', 'post status');
if ( is_sticky($post->ID) )
    $post_states['sticky'] = __('Sticky');

if ( 'future' === $post->post_status ) {
    $post_states['scheduled'] = __( 'Scheduled' );
}

if ( 'page' === get_option( 'show_on_front' ) ) {
    if ( intval( get_option( 'page_on_front' ) ) === $post->ID ) {
        $post_states['page_on_front'] = __( 'Front Page' );
    }

    if ( intval( get_option( 'page_for_posts' ) ) === $post->ID ) {
        $post_states['page_for_posts'] = __( 'Posts Page' );
    }
}

if ( intval( get_option( 'wp_page_for_privacy_policy' ) ) === $post->ID ) {
    $post_states['page_for_privacy_policy'] = __( 'Privacy Policy Page' );
}

/**
 * Filters the default post display states used in the posts list table.
 *
 * @since 2.8.0
 * @since 3.6.0 Added the `$post` parameter.
 *
 * @param array   $post_states An array of post display states.
 * @param WP_Post $post        The current post object.
 */
$post_states = apply_filters( 'display_post_states', $post_states, $post );

if ( ! empty($post_states) ) {
    $state_count = count($post_states);
    $i = 0;
    echo ' — ';
    foreach ( $post_states as $state ) {
        ++$i;
        ( $i == $state_count ) ? $sep = '' : $sep = ', ';
        echo "<span class="post-state">$state$sep</span>";
    }
}

enter image description here

For Example: This Code from Ultimate Member plugin

/**
     * Add a post display state for special UM pages in the page list table.
     *
     * @param array $post_states An array of post display states.
     * @param \WP_Post $post The current post object.
     *
     * @return mixed
     */
    public function add_display_post_states( $post_states, $post ) {

        foreach ( UM()->config()->core_pages as $page_key => $page_value ) {
            $page_id = UM()->options()->get( UM()->options()->get_core_page_id( $page_key ) );

            if ( $page_id == $post->ID ) {
                $post_states[ 'um_core_page_' . $page_key ] = sprintf( 'UM %s', $page_value['title'] );
            }
        }

        return $post_states;
    }

Leave a Comment