3

I have a custom post type named ÷project÷ which has 'capability_type' => 'post' while registering using ÷register_post_type÷.

in my theme files and also functions.php file, I have situations where I should check if the project to be edited is authored by the user or not. so I use this:

if ( !current_user_can( 'edit_post', $porject_id ) ) return;

where $project_id is post id of the project to be edited. for example I have a page template named single_project.php where I can show single posts in project post type to users and I want these people can see each project single page:

  1. administrators
  2. editors
  3. the contributer (post author)

but using the above code, It is ok with administrators and editors, while not for the contributer himself!

when I use var_dump( current_user_can( 'edit_post', $project_id ) ) with the contributer logged-in account, it returns false, while var_dump( current_user_can( 'edit_posts' ) ) (with trailing 's') returns true (but for every post, not just does for him).

Any help with this ??!

4

using map_meta_cap I added edit_post per post cap to user

function wp3344_map_meta_cap( $caps, $cap, $user_id, $args ){
    if ( 'edit_post' == $cap ) {
        $post = get_post( $args[0] );
        $post_type = get_post_type_object( $post->post_type );
        $caps = array();
        if ( $user_id == $post->post_author )
            $caps[] = $post_type->cap->edit_posts;
        else
            $caps[] = $post_type->cap->edit_others_posts;
    }
    return $caps;
}
add_filter( 'map_meta_cap', 'wp3344_map_meta_cap', 10, 4 );
|improve this answer|||||

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.