0

I have a custom post type for a portfolio that used to work and now it no longer shows up in the admin menu. It does however show up in the admin bar but not in the dashboard, only when viewing the site. If I click to go to the portfolio section to add an item, I get the Cheatin uh? error message. I really hope someone can help with this as I've spent hours trying to work this out. Thanks in advance!

<?php
function register_portfolio() {

    $labels = array( 
        'name' => __( 'Add New Portfolio', 'bk' ),
        'singular_name' => __( 'Portfolio', 'bk' ),
        'add_new' => __( 'Add New', 'bk' ),
        'add_new_item' => __( 'Add New Portfolio Item', 'bk' ),
        'edit_item' => __( 'Edit Portfolio', 'bk' ),
        'new_item' => __( 'New Portfolio', 'bk' ),
        'view_item' => __( 'View Portfolio', 'bk' ),
        'search_items' => __( 'Search Portfolio', 'bk' ),
        'not_found' => __( 'No Portfolio found', 'bk' ),
        'not_found_in_trash' => __( 'No Portfolio found in Trash', 'bk' ),
        'parent_item_colon' => __( 'Parent Portfolio:', 'bk' ),
        'menu_name' => __( 'Portfolio', 'bk' ),
    );
    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'supports' => array('title', 'editor', 'thumbnail'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'rewrite' => array('slug' => 'portfolio'),

        'query_var' => true,
        'can_export' => true,
        'map_meta_cap' => true,
        'capabilities' => array(
            'edit_post' => 'edit_portfolio',
            'edit_posts' => 'edit_portfolio',
            'edit_others_posts' => 'edit_other_portfolio',
            'publish_posts' => 'publish_portfolio',
            'edit_publish_posts' => 'edit_publish_portfolio',
            'read_post' => 'read_portfolio',
            'read_private_posts' => 'read_private_portfolio',
            'delete_post' => 'delete_portfolio',
            'delete_posts' => 'delete_portfolio',
            'delete_others_posts' => 'delete_others_portfolio',
            'delete_published_posts' => 'delete_published_portfolio'
        ),
        'capability_type' => array('portfolio', 'portfolio'),
    );
    register_post_type( 'bk-portfolio', $args );
}
add_action( 'init', 'register_portfolio' );

// Add custom taxonomies
function portfolio_create_taxonomies() {    

    $portfolio_type_labels = array(
        'name' => __( 'Portfolio Categories', 'taxonomy general name', 'bk' ),
        'singular_name' => __( 'Portfolio Category', 'taxonomy singular name', 'bk' ),
        'search_items' =>  __( 'Search Portfolio Categories', 'bk' ),
        'all_items' => __( 'All Portfolio categories', 'bk' ),
        'most_used_items' => null,
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Portfolio Category', 'bk' ),
        'update_item' => __( 'Update Portfolio Category', 'bk' ),
        'add_new_item' => __( 'Add New Portfolio Category', 'bk' ),
        'new_item_name' => __( 'New Portfolio Category', 'bk' ),
        'menu_name' => __( 'Categories', 'bk' ),
    );
    register_taxonomy('portfolio-category', 'bk-portfolio',array(
        'hierarchical' => true,
        'labels' => $portfolio_type_labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'portfolio-category' )
    ));


}
add_action( 'init', 'portfolio_create_taxonomies', 0 );


function manage_portfolio_capabilities() {
    // gets the role to add capabilities to
    $admin = get_role( 'administrator' );

    // replicate all the remapped capabilites from the custom post type portfolio
    $caps = array(
        'edit_portfolio',
        'edit_portfolio',
        'edit_other_portfolio',
        'publish_portfolio',
        'edit_published_portfolio',
        'read_portfolio',
        'read_private_portfolio',
        'delete_portfolio',
        'delete_portfolio',
        'delete_others_portfolio',
        'delete_published_portfolio'
    );
    // give all the capabilities to the administrator
    foreach ($caps as $cap) {
        $admin->add_cap( $cap );
    }

}
add_action( 'admin_init', 'manage_portfolio_capabilities');
1

Delete map_meta_cap. it will appear in your dashboard

    'map_meta_cap' => true,  //remove this line

why are you trying to restrict all those capabilities? IT look likes you've not given yourself capability to see the post with the restrictions there...

if you don't know why those are there, you may not need any of these lines: (SAVE A COPY OF THE PLUGIN BEFORE JUST IN CASE THOUGH AS THERE MAY BE ANOTHER FUNCTION THAT REQUIRES THIS FOR SOME REASON. ie you have a function at the end of the code that is looking at capabilities as well.)

    'map_meta_cap' => true,
    'capabilities' => array(
        'edit_post' => 'edit_portfolio',
        'edit_posts' => 'edit_portfolio',
        'edit_others_posts' => 'edit_other_portfolio',
        'publish_posts' => 'publish_portfolio',
        'edit_publish_posts' => 'edit_publish_portfolio',
        'read_post' => 'read_portfolio',
        'read_private_posts' => 'read_private_portfolio',
        'delete_post' => 'delete_portfolio',
        'delete_posts' => 'delete_portfolio',
        'delete_others_posts' => 'delete_others_portfolio',
        'delete_published_posts' => 'delete_published_portfolio'
    ),
    'capability_type' => array('portfolio', 'portfolio'),

additionally i'm a big proponent of namespace to reduce future conflicts. There will be lots of people and themes that use "portfolio" as their cpt.

your portfolio name is already identified (bk_portfolio instead of just portfolio) but your actual function isn't.

function register_portfolio() {

should be

function bk_register_portfolio() {

and you'll need to make sure to call the right function by changing

add_action( 'init', 'register_portfolio' );

to

add_action( 'init', 'bk_register_portfolio' );

LASTLY.

in your lables array you've named your portfolio "add new portfolio" that doesn't make sense. "name" label should be plural as well. Change it to Portfolios. and then you can change your add new lined to "Add new Portfolio"...(okay that one is just nit picky)

$labels = array( 
    'name' => __( 'Add New Portfolio', 'bk' ),  //change to Portfolios
    'singular_name' => __( 'Portfolio', 'bk' ),
    'add_new' => __( 'Add New', 'bk' ),  //Change to Add New Portfolio
    'add_new_item' => __( 'Add New Portfolio Item', 'bk' ),
    'edit_item' => __( 'Edit Portfolio', 'bk' ),
    'new_item' => __( 'New Portfolio', 'bk' ),
    'view_item' => __( 'View Portfolio', 'bk' ),
    'search_items' => __( 'Search Portfolio', 'bk' ),
    'not_found' => __( 'No Portfolio found', 'bk' ),
    'not_found_in_trash' => __( 'No Portfolio found in Trash', 'bk' ),
    'parent_item_colon' => __( 'Parent Portfolio:', 'bk' ),
    'menu_name' => __( 'Portfolio', 'bk' ),
);
  • You sir are a genius!!!! This isn't my code, I'm just taking a look for someone. What does 'map_meta_cap' do? If you don't mine me asking? – user53340 Nov 4 '16 at 17:55
  • see above i added a bit more info. It looks like the plugin was trying to map (map_meta_cap) capabilities that a normal post has to your portfolio post type. (if user can edit posts, they can edit portfolios...etc) – rudtek Nov 4 '16 at 18:15
  • Nice one thanks for the explanation. I made the amendment to capabilities while I was waiting for the reply, but I will change the naming too because you're right, it doesn't make sense at all. Plus when you go to the portfolio page, it says "Add New Portfolio" in the browser... lol Thanks again for all your help!! Much appreciated and a great in depth answer. ;) – user53340 Nov 4 '16 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.