Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6821523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:33:19+00:00 2026-05-26T21:33:19+00:00

I’m writing a plugin which creates a custom post_type. I’d also like the plugin

  • 0

I’m writing a plugin which creates a custom post_type. I’d also like the plugin to create a custom role which can only add/edit/delete the new post_type. I’ve tried several plugins (Role Scoper, Advanced Access manager) and they allow me to redefine or create new roles, but they don’t allow me to assign capabilities specific to the new post_type. For example, I want to allow the ability to add/edit my new post_type but NOT normal posts/pages.

From what I’ve read, I can add new roles with the add_role() function. One of the parameters of this function is an array of “capabilities” which appear to be defined here. I think what I need is to be able to add my capabilities that are specific to MY post_type. Is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:33:19+00:00Added an answer on May 26, 2026 at 9:33 pm

    Capabilities for Custom Post Types

    The function register_post_type() takes a $capabilities array as one of its (optional) arguments.

    It could look like so:

    $capabilities = array(
        'publish_posts' => 'publish_ypts',
        'edit_posts' => 'edit_ypts',
        'edit_others_posts' => 'edit_others_ypts',
        'delete_posts' => 'delete_ypts',
        'delete_others_posts' => 'delete_others_ypts',
        'read_private_posts' => 'read_private_ypts',
        'edit_post' => 'edit_ypt',
        'delete_post' => 'delete_ypt',
        'read_post' => 'read_ypt'
    );
    

    where “ypt” stands for “your post type”.

    Thereafter you could add a new role to your WordPress that has these exact capabilities (and possibly some more of the standard WordPress capabilities):

    add_role(
        'ypt_author',
        'Author of your post type',
        array(
            'publish_ypts' => true,
            'edit_ypts' => true,
            'edit_others_ypts' => true,
            'delete_ypts' => true,
            'delete_others_ypts' => true,
            'read_private_ypts' => true,
            'edit_ypt' => true,
            'delete_ypt' => true,
            'read_ypt' => true,
            // more standard capabilities here
        )
    );
    

    The latter can be done using plugins though, check out the Members plugin by Justin Tadlock, for instance.

    Thorough Example

    To give you a more concrete example:

    /* REGISTER POST TYPE */
    
    add_action('init', 'ypt_register');
    
    function ypt_register()
    {
    
        $labels = array(
            'name' => _x('YPTs', 'post type general name'),
            'singular_name' => _x('YPT', 'post type singular name'),
            'add_new' => _x('Add New YPT', 'Team item'),
            'add_new_item' => __('Add a new post of type YPT'),
            'edit_item' => __('Edit YPT'),
            'new_item' => __('New YPT'),
            'view_item' => __('View YPT'),
            'search_items' => __('Search YPTs'),
            'not_found' =>  __('No YPTs found'),
            'not_found_in_trash' => __('No YPTs currently trashed'),
            'parent_item_colon' => ''
        );
    
        $capabilities = array(
            // this is where the first code block from above goes
        );
    
        $args = array(
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => true,
            'capability_type' => 'ypt',
            'capabilities' => $capabilities,
            'hierarchical' => false,
            'menu_position' => null,
            'supports' => array( 'title', 'author', 'thumbnail' )
        ); 
    
        register_post_type( 'ypt' , $args );
    
        flush_rewrite_rules( false );
    }
    
    
    /* MAP META CAPABILITIES */
    
    add_filter( 'map_meta_cap', 'ypt_map_meta_cap', 10, 4 );
    
    function ypt_map_meta_cap( $caps, $cap, $user_id, $args )
    {
    
        if ( 'edit_ypt' == $cap || 'delete_ypt' == $cap || 'read_ypt' == $cap ) {
            $post = get_post( $args[0] );
            $post_type = get_post_type_object( $post->post_type );
            $caps = array();
        }
    
        if ( 'edit_ypt' == $cap ) {
            if ( $user_id == $post->post_author )
                $caps[] = $post_type->cap->edit_posts;
            else
                $caps[] = $post_type->cap->edit_others_posts;
        }
    
        elseif ( 'delete_ypt' == $cap ) {
            if ( $user_id == $post->post_author )
                $caps[] = $post_type->cap->delete_posts;
            else
                $caps[] = $post_type->cap->delete_others_posts;
        }
    
        elseif ( 'read_ypt' == $cap ) {
            if ( 'private' != $post->post_status )
                $caps[] = 'read';
            elseif ( $user_id == $post->post_author )
                $caps[] = 'read';
            else
                $caps[] = $post_type->cap->read_private_posts;
        }
    
        return $caps;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.