Support » Plugin: Co-Authors Plus » Associate authors programmatically

  • Resolved mabho


    Hi, I am trying to programmatically associate authors for a given post of type ‘laboratory’ (it is a custom post type already defined in the system). Basically, to make a long story short, I am using the following code (considering ‘$user_01->ID’ and ‘$user_02->ID’ are the IDs of the two users I want to associate as post authors):

      $my_post = array (
        'comment_status'    => 'closed',
        'ping_status'       => 'closed',
        'post_author'       => $user->ID,
        'post_title'        => $title,
        'post_status'       => 'draft',
        'post_type'         => 'laboratory',
      // Creates the post of type 'laboratory' and gets its ID right after.
      $post_id = wp_insert_post( $my_post );
      // ... and here is how I am trying to add coauthors:
      global $coauthors_plus;
      $coauthors_plus->add_coauthors( $post_id, array( $user_01->user_login, $user_02->user_login ), false );

    This doesn’t work. The post of type ‘laboratory’ is actually created, but the global admin user is assigned as author, instead of the two coauthors. I have also tried passing user IDs for the add_coauthors function, with no success:

    $coauthors_plus->add_coauthors( $post_id, array( $user_01->ID, $user_02->ID ), false );
    • This topic was modified 4 months, 1 week ago by  mabho.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Venutius


    I’m having the same problem, did you find a working solution?

    I think the the user setting should be $user->user_nicename but that does not work, tried appending the new user, that did not work either. Not getting any errors.



    Hey I got it working using the nicename. The problem I had was that I was triggering my action on save_post at the same priority as co-authors plus was also running it’s save_post rutine and it was overwriting mine.



    Hey, @venutius, thank you so much for sharing your experience and your solution for this topic. I left this issue aside for some time, but I will have to resume working on it soon and will share my experience here when I have news. Again, thank you, I will give your solution a try.

    @venutius, thank you for sharing your solution. After so long I have finally resumed working on this and can confirm your solution was effective for me.

    • This reply was modified 3 months, 1 week ago by  mabho.
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.