• officeninjas

    (@officeninjas)


    Unfortunately, I don’t know when this issue started happening. Since I am using a managed WordPress hosting provider it may have been due to the WordPress upgrade to 4.1.1.

    Steps to recreate issue:
    User A creates a post
    Save post (draft or publish, doesn’t matter)
    User A edits post and adds a co-author User B
    Save post
    User A edits post
    **BUG** Author shown is User A alone

    Results of Debugging
    After some debugging, I found the issue was with get_coauthors() from within function coauthors_meta_box. Since get_coauthors() is called in a few places, I presume, without any issues, the problem might be a timing issue when this particular call is made and not having the taxonomy “authors” available yet.

    The functions of the call stack are:
    co-authors() -> get_the_terms() -> get_object_term_cache() -> wp_cache_get()

    get_object_term_cache() returns a WP_error object with an error of “Invalid Taxonomy”.

    Workaround Temporary Solution
    I bypass the cache by commenting out get_coauthors() from within function coauthors_meta_box and added the following:

    $coauthor_terms = wp_get_object_terms( $post->ID, $coauthors_plus->coauthor_taxonomy);

    Then follow it by the following code that the get_coauthors() functions does in order to get the output in the appropriate format:

    if ( is_array( $coauthor_terms ) && !empty( $coauthor_terms ) ) {
      foreach( $coauthor_terms as $coauthor ) {
        $coauthor_slug = preg_replace( '#^cap\-#', '', $coauthor->slug );
        $post_author =  $coauthors_plus->get_coauthor_by( 'user_nicename', $coauthor_slug );
        // In case the user has been deleted while plugin was deactivated
        if ( !empty( $post_author ) )
          $coauthors[] = $post_author;
      }
    } else if ( !$coauthors_plus->force_guest_authors ) {
      if ( $post ) {
        $post_author = get_userdata( $post->post_author );
      } else {
        $post_author = get_userdata( $wpdb->get_var( $wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d", $post_id ) ) );
      }
      if ( !empty( $post_author ) )
        $coauthors[] = $post_author;
    } // the empty else case is because if we force guest authors, we don't ever care what value wp_posts.post_author has.

    I’m not well versed enough in the inner working of WordPress Core to quickly debug this further, but I hope my workaround solution helps anyone having the same issue.

    https://www.ads-software.com/plugins/co-authors-plus/

Viewing 1 replies (of 1 total)
  • Thread Starter officeninjas

    (@officeninjas)

    My hack doesn’t work for all cases. It turns out it is a race condition issue with init action. The fix for me was to change line 64 of co-authors-plus.php as follows.

    Original
    add_action( 'init', array( $this, 'action_init_late' ), 100 );

    New
    add_action( 'init', array( $this, 'action_init_late' ), 10 );

    Good luck!

Viewing 1 replies (of 1 total)
  • The topic ‘Post in edit mode defaults author to original author of post.’ is closed to new replies.