Support » Plugin: Co-Authors Plus » Order of authors not retained

  • After upgrading to wordpress 4.7, the order of the co-authors is not retained after re-ordering them and updating the post.

    • This topic was modified 3 years, 1 month ago by Haldaug.
Viewing 15 replies - 1 through 15 (of 16 total)
  • I have the same problem

    Same issue here. This is very problematic. I have over 3k posts that use this plugin and this appears to be impacting all of them. I looked into one such post and noticed that the term_order in the wp_term_relationships table has the correct order for the authors. So it appears that co-authors is not respecting the term order.

    Same issue. Posting here to be informed when a fix is available.

    Same here.
    Reordering won’t save, tried both quick edit and edit. It falls back to alphabetical order

    Same issue with me. Please help.

    Yes, same issue here

    same here

    Anyone found an fix?
    Thanks

    Me too. I have over 5600 posts. Has anyone found a solution? In my case, I have avatars and stars under each avatar indicating ratings for books. Now that the avatars are in alphabetical order, the stars are also not in the correct order.

    Example: http://www.fantasyliterature.com/reviews/the-fifth-season/

    • This reply was modified 3 years, 1 month ago by hooperkat.

    This appears to be an issue with WordPress 4.7 core, not the plugin. get_the_terms() in 4.7 does not return terms in the right order.

    Here’s a fix I’ve implemented in the get_coauthors() function, in template-tags.php in the plugin source.

    
    // Starting on line 19 in the current version
    if ( is_array( $coauthor_terms ) && ! empty( $coauthor_terms ) ) {
      
      // Collect all author term IDs
      $term_ids = array_map(function($el){
        return $el->term_id;
      }, $coauthor_terms);
      
      // Get order of term IDs for this post
      $sql_get_author_order = sprintf(
        "SELECT term_taxonomy_id, term_order
        FROM %s
        WHERE object_id = %d
        AND term_taxonomy_id IN (%s)
        ORDER BY term_order ASC",
        $wpdb->term_relationships,
        $post_id,
        join(', ', $term_ids)
      );
      
      // Organize order by term ID
      $author_order_data = $wpdb->get_results($sql_get_author_order);
      if(!empty($author_order_data)) {
        $author_order_data_ids = [];
        foreach($author_order_data as $order_data) {
          $author_order_data_ids[$order_data->term_taxonomy_id] = $order_data->term_order;
        }
        
        foreach($coauthor_terms as &$coauthor) {
          $coauthor->order = $author_order_data_ids[$coauthor->term_id];
        }
      }
      
      // Sort authors by order
      usort($coauthor_terms, function($a, $b){
        return ((int) $a->order > (int) $b->order) ? 1 : -1;
      });
      
      foreach ( $coauthor_terms as $coauthor ) {
    // ...
    

    Hopefully 4.7.1 will fix this and render this hack obsolete!

    Correction: don’t use &$coauthor when iterating over the terms, otherwise there will be problems with multiple authors.

    foreach($coauthor_terms as &$coauthor_term) {
      $coauthor_term->order = $author_order_data_ids[$coauthor_term->term_id];
    }

    Thanks for the hack, Alan!

    It didn’t work for me, but after a few modifications I got it to work. Here is my modified version:

    if ( is_array( $coauthor_terms ) && ! empty( $coauthor_terms ) ) {
    		  
    		  // Collect all author term IDs
    		  $term_ids = array_map(function($el){
    			return $el->term_id;
    		  }, $coauthor_terms);
    		  
    		  // Collect all taxonomy term IDs per author
    		  $sql_get_term_taxonomy_ids = sprintf(
    			"SELECT term_taxonomy_id
    			FROM %s
    			WHERE term_id IN (%s)
    			",
    			$wpdb->term_taxonomy,
    			join(', ', $term_ids)
    		  );
    		  
    		  $term_taxonomy_ids = $wpdb->get_results($sql_get_term_taxonomy_ids);
    		  
    		  $term_tax_ids = array_map(function($el){
    			return $el->term_taxonomy_id;
    		  }, $term_taxonomy_ids);
    		  
    		  // Get order of term taxonomy IDs for this post
    		  $sql_get_author_order = sprintf(
    			"SELECT term_taxonomy_id, term_order
    			FROM %s
    			WHERE object_id = %d
    			AND term_taxonomy_id IN (%s)
    			ORDER BY term_order ASC",
    			$wpdb->term_relationships,
    			$post_id,
    			join(', ', $term_tax_ids)
    		  );
    		  
    		  // Organize order by term ID
    		  $author_order_data = $wpdb->get_results($sql_get_author_order);
    		  if(!empty($author_order_data)) {
    			$author_order_data_ids = [];
    			foreach($author_order_data as $order_data) {
    			  $author_order_data_ids[$order_data->term_taxonomy_id] = $order_data->term_order;
    			}
    			
    			foreach($coauthor_terms as &$coauthor_term) {
    			  $coauthor_term->order = $author_order_data_ids[$coauthor_term->term_taxonomy_id];
    			}
    		  }
    		  
    		  // Sort authors by order
    		  usort($coauthor_terms, function($a, $b){
    			return ((int) $a->order > (int) $b->order) ? 1 : -1;
    		  });
    		  
    		  
    			foreach ( $coauthor_terms as $coauthor ) {
                     // ..

    As you can see, I iterated over the term_taxonomy_id’s instead of term_id.

    Actually, scratch this hack and substitute it for this much simpler hack courtesy of Miles Blackwood Robinson over at this plugin’s Github repository: https://github.com/Automattic/Co-Authors-Plus/issues/390

    The fix is as follows. Simply substitute line 17 in the file template-tags.php from:

    $coauthor_terms = get_the_terms( $post_id, $coauthors_plus->coauthor_taxonomy );

    to:

    $coauthor_terms = wp_get_object_terms( $post_id, $coauthors_plus->coauthor_taxonomy, array( 'orderby' => 'term_order', 'order' => 'ASC' ) );

    • This reply was modified 3 years, 1 month ago by Haldaug. Reason: Missing space
    • This reply was modified 3 years, 1 month ago by Haldaug.
    • This reply was modified 3 years, 1 month ago by Haldaug. Reason: Corrected the attribution

    Thanks, everyone! I’m so happy that WP still had the right author orders in the database. All is well now!

    Yay! And whew! I have thousands of posts using this plug-in. Thanks guys!

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Order of authors not retained’ is closed to new replies.