Change WordPress post-state in Admin Area

Im wondering if it’s possible to add custom post state’s to other pages?
Like Frontpage and Blog

I mean the black text: –Voorpagina (Frontpage)

Post State WordPresss

I have tried this in functions.php.

add_filter('display_post_states', 'wpsites_custom_post_states');

function wpsites_custom_post_states($states) { 
    global $post; 
    if( ('page' == get_post_type($post->ID) ) 
        && ( '/themes/Lef-en-Liefde/woningoverzicht.php' == get_page_templ‌​ate_slug( $post->ID ) ) ) {

            $states[] = __('Custom state'); 

    } 
} 

But then the original post states (frontpage and blog) are gone.

Thanks in advance,

Gino

3 Answers
3

Your function doesn’t return a value, so you are effectively wiping the existing states.

Also, this hook passes you the current post as an object, so you can use that instead of the global $post.

Additionally get_page_templ‌​ate_slug returns a path that is relative to your theme’s root, so if your theme directory is called Lef-en-Liefde and the template file placed at the top level of that directory is called woningoverzicht.php, then your condition '/themes/Lef-en-Liefde/woningoverzicht.php' == get_page_templ‌​ate_slug($post->ID) can never be true.

So, correcting/changing for those points, this should work:

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

function wpse240081_custom_post_states( $states, $post ) { 
    
    if ( ( 'page' == get_post_type( $post->ID ) ) 
        && ( 'woningoverzicht.php' == get_page_templ‌​ate_slug( $post->ID ) ) ) {

            $states[] = __('Custom state'); 

    } 

    return $states;
} 

Leave a Comment