• Resolved bearshang

    (@bearshang)


    I put all the fields of register_post_type into the database, and then through the foreach stored multiple arrays, the results of my custom post type can not show in the admin.

    
    add_action( 'init', array( $this, 'tn_init' ), 99 );
    public function tn_init()
    {
       // Load WP custom post type
       new \Trueniu\Post\PostInit();
    }
    
    <?php
    namespace Trueniu\Post;
    class PostInit
    {
        public function __construct()
        {
            $tnPostType = get_option( 'tn_post_options' );
    
            if ( isset( $tnPostType ) && !empty( $tnPostType ) ) {
                foreach ( $tnPostType as $val ) {
                    if ( isset( $val['status'] ) && $val['status'] ) {
                        $labels = array(
                            'name'                  => $val['plural_name'],
                            'singular_name'         => $val['singular_name'],
                            'menu_name'             => __( $val['menu_name'], 'trueniu-init' ),
                            'name_admin_bar'        => __( $val['name_admin_bar'], 'trueniu-init' ),
                            'archives'              => __( $val['archives'], 'trueniu-init' ),
                            'attributes'            => __( $val['attributes'], 'trueniu-init' ),
                            'parent_item_colon'     => __( $val['parent_item_colon'], 'trueniu-init' ),
                            'all_items'             => __( $val['all_items'], 'trueniu-init' ),
                            'add_new_item'          => __( $val['add_new_item'], 'trueniu-init' ),
                            'add_new'               => __( $val['add_new'], 'trueniu-init' ),
                            'new_item'              => __( $val['new_item'], 'trueniu-init' ),
                            'edit_item'             => __( $val['edit_item'], 'trueniu-init' ),
                            'update_item'           => __( $val['update_item'], 'trueniu-init' ),
                            'view_item'             => __( $val['view_item'], 'trueniu-init' ),
                            'view_items'            => __( $val['view_items'], 'trueniu-init' ),
                            'search_items'          => __( $val['search_items'], 'trueniu-init' ),
                            'not_found'             => __( $val['not_found'], 'trueniu-init' ),
                            'not_found_in_trash'    => __( $val['not_found_in_trash'], 'trueniu-init' ),
                            'featured_image'        => __( $val['featured_image'], 'trueniu-init' ),
                            'set_featured_image'    => __( $val['set_featured_image'], 'trueniu-init' ),
                            'remove_featured_image' => __( $val['remove_featured_image'], 'trueniu-init' ),
                            'use_featured_image'    => __( $val['use_featured_image'], 'trueniu-init' ),
                            'insert_into_item'      => __( $val['insert_into_item'], 'trueniu-init' ),
                            'uploaded_to_this_item' => __( $val['uploaded_to_this_item'], 'trueniu-init' ),
                            'items_list'            => __( $val['items_list'], 'trueniu-init' ),
                            'items_list_navigation' => __( $val['items_list_navigation'], 'trueniu-init' ),
                            'filter_items_list'     => __( $val['filter_items_list'], 'trueniu-init' ),
                        );
                        $args = array(
                            'label'                 => $val['singular_name'],
                            'description'           => $val['description'],
                            'labels'                => $labels,
                            'supports'              => $val['supports'],
                            'taxonomies'            => explode( ',', $val['taxonomies'] ),
                            'hierarchical'          => false,
                            'public'                => $val['public'] == '1' ? 'true' : 'false',
                            'show_ui'               => $val['show_ui'] == '1' ? 'true' : 'false',
                            'show_in_menu'          => $val['show_in_menu'] == '1' ? 'true' : 'false',
                            'menu_position'         => $val['menu_position'],
                            'menu_icon'             => $val['menu_icon'],
                            'show_in_admin_bar'     => $val['show_in_admin_bar'] == '1' ? 'true' : 'false',
                            'show_in_nav_menus'     => $val['show_in_nav_menus'] == '1' ? 'true' : 'false',
                            'can_export'            => $val['can_export'] == '1' ? 'true' : 'false',
                            'has_archive'           => $val['has_archive'] == '1' ? 'true' : 'false',
                            'exclude_from_search'   => $val['exclude_from_search'] == '1' ? 'true' : 'false',
                            'publicly_queryable'    => true,
                            'query_var'             => true,
                            'capability_type'       => $val['capability_type'],
                        );
                        register_post_type( $val['post_type'], $args );
                    }
                }
            }
        }
    }
    
Viewing 8 replies - 1 through 8 (of 8 total)
  • Can you use print_r( $tnPostType ) and show the result?

    Thread Starter bearshang

    (@bearshang)

    @truongwp

    
    
    Array
    (
        [video] => Array
            (
                [post_type] => video
                [description] => Custom post type
                [singular_name] => video
                [plural_name] => videos
                [taxonomies] => video_cat
                [status] => 1
                [menu_name] => Video
                [name_admin_bar] => Video
                [archives] => Item Archives
                [attributes] => Item Attributes
                [parent_item_colon] => Parent Item:
                [all_items] => All Items
                [add_new_item] => Add New Item
                [add_new] => Add New
                [new_item] => New Item
                [edit_item] => Edit Item
                [update_item] => Update Item
                [view_item] => View Item
                [view_items] => View Items
                [search_items] => Search Item
                [not_found] => Not found
                [not_found_in_trash] => Not found in Trash
                [featured_image] => Featured Image
                [set_featured_image] => Set featured image
                [remove_featured_image] => Remove featured image
                [use_featured_image] => Use as featured image
                [insert_into_item] => Insert into item
                [uploaded_to_this_item] => Uploaded to this item
                [items_list] => Items list
                [items_list_navigation] => Items list navigation
                [filter_items_list] => Filter items list
                [supports] => Array
                    (
                        [0] => title
                        [1] => editor
                        [2] => revisions
                        [3] => excerpt
                        [4] => author
                        [5] => thumbnail
                        [6] => comments
                        [7] => page-attributes
                    )
    
                [exclude_from_search] => 1
                [can_export] => 1
                [has_archive] => 1
                [public] => 1
                [show_ui] => 1
                [show_in_menu] => 1
                [menu_position] => 5
                [menu_icon] => 
                [show_in_admin_bar] => 0
                [show_in_nav_menus] => 0
                [capability_type] => post
            )
    
        [post_1] => Array
            (
                [post_type] => post_1
                [description] => Custom post type
                [singular_name] => Service
                [plural_name] => Services
                [taxonomies] => service_cat
                [status] => 1
                [menu_name] => Service
                [name_admin_bar] => Service
                [archives] => Item Archives
                [attributes] => Item Attributes
                [parent_item_colon] => Parent Item:
                [all_items] => All Items
                [add_new_item] => Add New Item
                [add_new] => Add New
                [new_item] => New Item
                [edit_item] => Edit Item
                [update_item] => Update Item
                [view_item] => View Item
                [view_items] => View Items
                [search_items] => Search Item
                [not_found] => Not found
                [not_found_in_trash] => Not found in Trash
                [featured_image] => Featured Image
                [set_featured_image] => Set featured image
                [remove_featured_image] => Remove featured image
                [use_featured_image] => Use as featured image
                [insert_into_item] => Insert into item
                [uploaded_to_this_item] => Uploaded to this item
                [items_list] => Items list
                [items_list_navigation] => Items list navigation
                [filter_items_list] => Filter items list
                [supports] => Array
                    (
                        [0] => title
                        [1] => editor
                        [2] => revisions
                        [3] => excerpt
                        [4] => author
                        [5] => thumbnail
                        [6] => comments
                        [7] => page-attributes
                    )
    
                [exclude_from_search] => 1
                [can_export] => 1
                [has_archive] => 0
                [public] => 1
                [show_ui] => 1
                [show_in_menu] => 1
                [menu_position] => 10
                [menu_icon] => dahsicons-admin-post
                [show_in_admin_bar] => 0
                [show_in_nav_menus] => 1
                [capability_type] => page
            )
    
    )
    
    • This reply was modified 7 years, 5 months ago by bearshang.
    Thread Starter bearshang

    (@bearshang)

    I know why, when i use
    $ val [‘show_in_admin_bar’] == ‘1’? ‘True’: ‘false’,
    he outputs a string, not a boolean

    Should be written like this:
    $val [‘show_in_admin_bar’] == ‘1’? True : false,

    @bearshang I think 'True' of 'false' will be treated as true when convert to boolean. I don’t think that is the problem.

    Thread Starter bearshang

    (@bearshang)

    @truongwp

    false and ‘false’ is not same
    ‘false’ only return true
    false return false

    • This reply was modified 7 years, 5 months ago by bearshang.

    @bearshang Yes. I know. '1', 'true' or 'false' always returns true. So your post type should be shown in admin menu

    Thread Starter bearshang

    (@bearshang)

    I probably understand what you said,

    I do not understand it,but when i do that, it shows everything is normal, but when i put the boolean as a string when he does not work, I think WP will determine the type of value to decide whether to continue working

    Yes. I found the problem here:

    
    if ( $ptype_obj->show_in_menu !== true )
    		continue;
    

    WordPress use Not Identical operator instead of Not Equal to check when render the admin menu. I tested with value is 1 and it also doesn’t work.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘register_post_type() not show menu in admin.’ is closed to new replies.