0

I'm on WordPress 4.7.4 and on my custom post types, I cannot assign tags as a regular user.

I have used CPTUI for creating everything, have mapped and created custom capabilities, have assigned roles to the custom caps, and have verified the user's roles are correct using get_userdata(), but I can't seem to get this working.

On the CPT, the taxonomies variable has post_tag set and I can see the tags meta box, I just can't do anything with it. I have tried to separate the term capabilities using the function below and the user has the assign_post_tags role, but it doesn't seem to do anything. Any help would be greatly appreciated.

Register CPT:

function cptui_register_my_cpts_listing() {

    /**
     * Post Type: Listings.
     */

    $labels = array(
        "name" => __( 'Listings', 'text-domain' ),
        "singular_name" => __( 'Listing', 'text-domain' ),
    );

    $args = array(
        "label" => __( 'Listings', 'text-domain' ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => "archive-listing",
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "listing",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "listing", "with_front" => true ),
        "query_var" => "listing",
        "supports" => array( "title", "editor", "thumbnail" ),
        "taxonomies" => array( "post_tag", "location" ),
    );

    register_post_type( "listing", $args );
}

add_action( 'init', 'cptui_register_my_cpts_listing' );

Changing caps

function set_builtin_tax_caps() {
    $tax = get_taxonomy('post_tag');
    $tax->cap->manage_terms = 'manage_post_tags';
    $tax->cap->edit_terms = 'edit_post_tags';
    $tax->cap->delete_terms = 'delete_post_tags';
    $tax->cap->assign_terms = 'assign_post_tags';
    $tax = get_taxonomy('category');
    $tax->cap->manage_terms = 'manage_categories';
    $tax->cap->edit_terms = 'edit_categories';
    $tax->cap->delete_terms = 'delete_categories';
    $tax->cap->assign_terms = 'assign_categories';
}
add_action('init', 'set_builtin_tax_caps');
1

It seems that although WP core creates four capability mappings for the built-in post_tag taxonomy (manage_terms => manage_post_tags, edit_terms => edit_post_tags, delete_terms => delete_post_tags, assign_terms => assign_post_tags) when it is registered, it uses different values when checking if the user has one of those capabilities.

If you look at the implementation of the map_meta_cap function in WP core (in wp-includes/capabilities.php lines 513-523), you can see that the edit_posts capability is returned for assign_post_tags and manage_categories is returned for manage_post_tags, edit_post_tags and delete_post_tags. To fix this, you can add a filter and return the expected values:

add_filter( 'map_meta_cap', function( $caps, $cap, $user_id, $args ) {
    $caps_to_fix = [
        'manage_post_tags',
        'edit_post_tags',
        'delete_post_tags',
        'assign_post_tags',
    ];

    if ( in_array( $cap, $caps_to_fix ) ) {
        $caps = [ $cap ];
    }

    return $caps;
}, 10, 4 );
0

No matter what I tried, I couldn't get this working. I don't believe manually changing the terms are supported at this time for the post_tag taxonomy. Even if the user had the proper role and permission, you could not assign or edit post tags. The only solution I found was to create and register an entirely new taxonomy called "tags", define the custom capabilities, and set hierarchical to false.

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.