TransWikia.com

Can multiple authors be assigned on a single custom post?

WordPress Development Asked by Vishal Tanna on December 19, 2021

I have custom post types that I would like to make editable by several users. I created one custom post type using the Toolset plugin. wp_update_post only allows us to set single author:

$user_id = array();
$userIds = get_field("agency_to_author",$_POST['post_ID']); // Get Multiple User assing using acf field.
foreach ($userIds as $key => $value) {
     $user_id[] = $value['ID'];
}
if(!empty($user_id)){
     $arg = array(
         'ID' => $_POST['post_ID'],
         'post_author' => $user_id,
     );
     wp_update_post( $arg );    
}

One Answer

Easy way: use this plugin https://wordpress.org/plugins/user-role-editor/

More complicated way: You can use functions.php to add all of the custom capabilities to a specific roles. But ofc you need a name of these capabilities.

This code is usefull if you want to add X same capabilities to X roles.

Add to functions.php

   function wphm_add_custom_capabilities_to_roles($roles, $capabilities) {
     foreach($roles as $the_role) {
       $role = get_role($the_role);
       foreach($capabilities as $capability) {
         $role -> add_cap($capability);
       }
     }
   }

  wphm_add_custom_capabilities_to_roles(
    array(  // these roles will recieve capabilities
      'custom_user_role_wphm_support',
      'administrator'
    ),
    array( // capabilities
      "read_wphm_custom",
      "read_private_wphm_custom",
      "edit_wphm_custom",
      "edit_others_wphm_custom",
      "edit_published_wphm_custom",
      "publish_wphm_custom",
      "delete_others_wphm_custom",
      "delete_private_wphm_custom",
      "delete_published_wphm_custom"
    )
  );

To remove a capability with this loop just modify a line $role -> add_cap($capability); to $role -> remove_cap($capability)

Answered by Krystian Kupiec on December 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP