When creating content, there might be a situation when many users contribute to writing a post. Normally, authors will list their names as contributors. But if you do it manually, that may take time. So why don't you try creating a contributors list automatically to save time? To do it, we can get the list of users who edited the post in Revision, then display it on the posts page.
First of all, let's learn about how Revision works, and why we can rely on this feature to get the contributors’ lists of posts!
How Does Revision Work?
Revision is a default WordPress feature that helps you access the drafts that are saved manually or automatically. Each time you save a post, a corresponding draft will be created. You can restore the previous post content with this draft.
Therefore, Revision will save all the data of changes including who edited the post. Thanks to this mechanism, we can get the contributors lists saved in Revision and then show it on the post.
Step 1: Get the Contributors Lists in Revision
First, get the lists of users who edited the post (that is saved in Revision) by adding this code to the functions.php
file:
function get_the_modified_authors(){ global $wpdb; $results = $wpdb->get_results( $wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE (post_type = '%s' AND ID = %d) OR (post_type = 'revision' AND post_parent = %d) GROUP BY post_author", get_post_type( get_the_ID() ), get_the_ID(), get_the_ID() ) ); echo '<ul>'; foreach($results as $row){ $authors = get_the_author_meta('display_name', $row->post_author ); echo '<li>' . $authors . '</li>'; } echo '</ul>'; }
Explanation:
$results = $wpdb->get_results( $wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE (post_type = '%s' AND ID = %d) OR (post_type = 'revision' AND post_parent = %d) GROUP BY post_author", get_post_type( get_the_ID() ), get_the_ID(), get_the_ID() ) ); //
: This code is to get the ID list of users who edited the post and then merge the duplicate IDs together.foreach($results as $row){
: This loop will get the names of users corresponding to the IDs above.
$authors[] = get_the_author_meta('display_name', $row->post_author );
} //
Step 2: Show the Contributors List on the Post
After you got the contributors lists saved in Revision, the next thing to do is showing it on the post.
Go to the file that contains the single post page content, choose the position you want to show the contributors list, and then insert the code below:
<div class="list-contributors"> <h3>Contributors list</h3> <?php echo get_the_modified_authors(); ?> </div>
Explanation:
Contributors list
is the name of the list that will be displayed on the front end. You can give this contributor list any name you want.
Note: The file containing the single post page content will be different depending on the theme that you’re using. For example, I’m using the eStar theme (you can try this free theme here), and I need to edit the post.php
file.
Here is the contributors’ list (with three users contributed to the post) on a single post page of my website:
If you want the contributors’ list to be more beautiful and attractive, you can style it yourself. That’s done! Easy peasy, right?
Last Words
Revision not only helps you create drafts to review and restore the previous content but also helps you get the contributors’ lists of posts. You can get these contributors lists quickly and show them automatically on your posts within some lines of code. That trick will be extremely helpful if you have a multi-author site.
If you follow this tutorial strictly, it will be so easy. Good luck and have fun building WordPress websites!