How can i limit a user in wordpress

Can any one please tell me how can i limit a user to edit only his post.I used the role editor plugin but it allow the user to edit all users post.I’m creating a classified site plugin where a user can post(custom post type)and he can edit his post.

Related posts

Leave a Reply

2 comments

  1. You can limit a user to only edit their own posts using this bit of code.

    function my_authored_content($query) {
    
    //get current user info to see if they are allowed to access ANY posts and pages
    $current_user = wp_get_current_user();
    // set current user to $is_user
    $is_user = $current_user->user_login;
    
    //if is admin or 'is_user' does not equal #username
    if (!current_user_can('manage_options')){
        //if in the admin panel
        if($query->is_admin) {
    
            global $user_ID;
            $query->set('author',  $user_ID);
    
        }
        return $query;
    }
    return $query;
    }
    add_filter('pre_get_posts', 'my_authored_content');
    
    function remove_menu_items() {
    $current_user = wp_get_current_user();
    if ( !current_user_can( 'manage_options' ) ) {
        //hides comments menu
        remove_menu_page( 'edit-comments.php' );
        // hides posts menu
        remove_menu_page( 'edit.php' );
        hides pages menu
        remove_menu_page( 'edit.php?post_type=page' );
    }
    }
    add_action( 'admin_menu', 'remove_menu_items' );
    

    Hope this helps you 🙂