How to create a Page alias in WordPress

enter image description here

In the image you see… WordPress has alias for Pages like Home Page, Checkout Page, Privacy Policy Page and any appear beside the page you assign them.

How to create this feature in WordPress? If I want to alias a page like XYZ Page and when I assign any page to that alias… XYZ Page appears beside the assigned page.

1 Answer
1

Those are actually post states; not aliases.. And you can do it through the display_post_states filter, like so, where we check if the post ID ($post->ID) is 123 and if so, we assign the XYZ Page state to that post (which could be a Page, Custom Post Type, etc.):

add_filter( 'display_post_states', 'my_post_states', 10, 2 );
function my_post_states( $post_states, $post ) {
    if ( 123 === $post->ID ) {
        $post_states['xyz_page'] = 'XYZ Page';
    }

    return $post_states;
}

And for reference, this is the conditional which WordPress uses for the “Front Page” and “Posts Page” states:

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' );
    }
}

Leave a Comment