admin管理员组

文章数量:1124691

I have created two post types, but the problem is that both post types are shown in both menu menu of admin panel regardless of their type, and they are both shown in the standard posts menu.

I would like there to be a menu for each type of post. How to do it?

add_action('init', 'register_post1', 9);
function register_post1(){
    register_post_type( 'post2', array(
        'label'               => null,
        'description'         => '',
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_rest'        => true,
        'menu_position'       => 6,
        'menu_icon'           => null,
        'taxonomies'          => ['taxo1'], //'big_event_name', 
        'capability_type'   => 'post',
        //'capabilities'      => 'post',
        'map_meta_cap'      => true,
        'hierarchical'        => true,
        'has_archive'         => false,
        'rewrite'             => true,
        'query_var'           => true,
    ));
}
add_action('init', 'register_post_2', 9);
function register_post_2(){
    register_post_type( 'post1', array(
        'label'               => null,
        'description'         => '',
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_rest'        => true,
        'menu_position'       => 7,
        'menu_icon'           => null,
        'taxonomies'          => ['taxo2'],
        'capability_type'   => 'post',
        //'capabilities'      => 'post',
        'map_meta_cap'      => true,
        'hierarchical'        => false,
        'has_archive'         => false,
        'rewrite'             => true,
        'query_var'           => true,
    ));
}

I have created two post types, but the problem is that both post types are shown in both menu menu of admin panel regardless of their type, and they are both shown in the standard posts menu.

I would like there to be a menu for each type of post. How to do it?

add_action('init', 'register_post1', 9);
function register_post1(){
    register_post_type( 'post2', array(
        'label'               => null,
        'description'         => '',
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_rest'        => true,
        'menu_position'       => 6,
        'menu_icon'           => null,
        'taxonomies'          => ['taxo1'], //'big_event_name', 
        'capability_type'   => 'post',
        //'capabilities'      => 'post',
        'map_meta_cap'      => true,
        'hierarchical'        => true,
        'has_archive'         => false,
        'rewrite'             => true,
        'query_var'           => true,
    ));
}
add_action('init', 'register_post_2', 9);
function register_post_2(){
    register_post_type( 'post1', array(
        'label'               => null,
        'description'         => '',
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_rest'        => true,
        'menu_position'       => 7,
        'menu_icon'           => null,
        'taxonomies'          => ['taxo2'],
        'capability_type'   => 'post',
        //'capabilities'      => 'post',
        'map_meta_cap'      => true,
        'hierarchical'        => false,
        'has_archive'         => false,
        'rewrite'             => true,
        'query_var'           => true,
    ));
}
Share Improve this question asked Feb 16, 2024 at 15:05 upkagefaupkagefa 312 bronze badges 2
  • I'm not sure exactly what you're seeing but try making your new posts something other than post. Just for brevity as well, you could put both register_post_types together in a single wrapper and call just the one at init. (that part wouldn't likely fix your problem though. – rudtek Commented Feb 16, 2024 at 15:57
  • I declared two custom post types post1 and post2. Two additional menus have appeared in the admin panel. That is, now there are three menu items: the default one (post) and two custom ones (post1 and post2). When I go to the post2 menu item, I want to see only post2 custom post type, but I see all created posts of all types. – upkagefa Commented Feb 19, 2024 at 5:27
Add a comment  | 

2 Answers 2

Reset to default 1

There is an assorted list of issues on your code.

When I load your code I'm not seeing the same thing you do. I'm seeing it create a second posts section and a second pages section, which is what it should be doing because you set one to hierarchical and one not.

The primary issue is that you're using capabilities wrong. it uses default WordPress Capabilities. Perhaps you meant edit_posts? If you don't want any special capabilities to use this post, then just delete that line in both register_post_type args. In fact, delete all cap lines.

Next is you're not setting labels, in fact you're deleting your labels. The point of of custom post types is to customize. Give the post a meaningful name here:

'label'               => NULL,

should be

'label'               => 'posttype2',

Better yet change this to assign specific labels in a labels ARRAY (see the documentation for what all you can do here)

Then you set menu icon to NULL. If you want a generic menu icon, get rid of this line. if you're trying to add one with CSS, then replace with 'none'.

Lastly, you set rewrite and query_var to TRUE. I'm not sure why. If you're not customizing it, just let it be and delete these lines.

I would take a minute to look over the register_post_type options and add a labels array and I would also take the opportunity to create a more custom post_type name when registering it as well.

TL / DR

This should be your code below. (I've added sample labels but you should replace with your own post names).

add_action('init', 'register_post1');
function register_post1(){
    register_post_type( 'post2', array(
        'label'               => 'books',
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_rest'        => true,
        'menu_position'       => 6,
        'taxonomies'          => array('taxo1'), //'big_event_name', 
        'hierarchical'        => true,
        'has_archive'         => false
    ));
}
add_action('init', 'register_post_2');
function register_post_2(){
    register_post_type( 'post1', array(
        'label'               => 'grains',
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_rest'        => true,
        'menu_position'       => 7,
        'taxonomies'          => array('taxo2'),
        'hierarchical'        => false,
        'has_archive'         => false
    ));
}

@rudtek Thank you very much for your detailed answer and your advice, I will use them. I'll be sure to add a point to your answer when I can. It’s a bit awkward for me to say this, but the mistake was elsewhere. In order to display posts of a new type, it was necessary to use the pre_get_posts hook, which I used like this:

function add_event_declaration_post_type( $query ) {
    if ($query->is_main_query()) 
        $query->set( 'post_type', array( 'post', 'event_declaration' ) );
    return $query;
}
add_action( 'pre_get_posts', 'add_event_declaration_post_type' );

The problem was $query->is_main_query() verification. In addition to the check for the main queue, it turned out that we need to introduce another check $query->is_home(), otherwise it will work in the admin menu too.

Thus, the correct addition of custom post types to the index page would be:

function add_new_post_type_p( $query ) {
    if ($query->is_main_query() && $query->is_home()) 
        $query->set( 'post_type', array( 'post', 'post1', 'post2' ) );
    return $query;
}
add_action( 'pre_get_posts', 'add_new_post_type_p' );

本文标签: Сustom posts are displayed in the wrong menus