Using menu_position to add two entries between Dashboard and Posts

We're looking to add two new (custom post type) entries between Dashboard and Posts.

In this project, for the end user, two items are going to be by far the most used items in his dashboard. So we want them at the top.

These seem to be the built in menu_position settings:

This only gives a single slot, menu_position=3, between Dashboard and Posts. And we need two slots!

What we've tried:

1) Using decimals - as strings - is sometimes suggested (ie. 3.1 and 3.2). But this definitely doesn't work in current WordPress. Our menu entries drop down to the default position.

2) Setting both of them to 3. This isn't allowed. It results in one of them dropping down to the default position.

Other suggestions on this site involve a custom function which grabs all the menu items one by one and reorders them individually but this looks fragile. What if other items are added at a later date? Can't we just reorder Dashboard up (or Posts down) to make space?

Topic menu-order admin-menu custom-post-types Wordpress

Category Web


The order of the menu items can be changed using the filter menu_order

add_filter( 'menu_order', 'se354759_menu_order' );
add_filter( 'custom_menu_order', '__return_true' );

function se354759_menu_order ($menu_order)
{
    $cpts = [
        'edit.php?post_type=' . 'custom-post-type', 
        'edit.php?post_type=' . 'another-cpt'
    ];
    //
    // remove and save first item ("dashboard") in variable
    $first_item = array_shift( $menu_order );
    foreach( $cpts as $ctp )
    {
        $idx = array_search( $ctp, $menu_order );
        if ( $idx === false )
            continue;
        //
        // remove CPT menu item from array
        unset( $menu_order[$idx] );
        //
        // add CPT item to the beginning of the array
        array_unshift( $menu_order, $ctp );
    }
    //
    // re-add "dashboard" item
    array_unshift( $menu_order, $first_item );

    return $menu_order;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.