How to Create Contributors Lists for Posts
Tags: wordpress
Published: 2020-12-08 03:57:32

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 <strong>creating a contributors list automatically</strong> 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.

<!--more-->

First of all, let's learn about how Revision works, and why we can rely on this feature to <strong>get the contributors' lists of posts</strong>!

<h2>How Does Revision Work?</h2>

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, <a href="https://gretathemes.com/why-you-need-revision-in-wordpress/">Revision</a> 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.

<img class="aligncenter" src="https://i.imgur.com/zTRUpCr.jpg" alt="How to Create Contributors Lists for Posts" width="5000" height="3333" />

<h2>Step 1: Get the Contributors Lists in Revision</h2>

First, get the lists of users who edited the post (that is saved in Revision) by adding this code to the <code>functions.php</code> file:

<pre>function get_the_modified_authors(){

    global $wpdb;

    $results = $wpdb-&gt;get_results( $wpdb-&gt;prepare("SELECT post_author FROM $wpdb-&gt;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 '&lt;ul&gt;';

    foreach($results as $row){

        $authors =  get_the_author_meta('display_name', $row-&gt;post_author );

        echo '&lt;li&gt;' . $authors . '&lt;/li&gt;';

    }

    echo '&lt;/ul&gt;';

}

</pre>

<strong> Explanation</strong>:

<ul>

   <li><code>$results = $wpdb-&gt;get_results( $wpdb-&gt;prepare("SELECT post_author FROM $wpdb-&gt;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() ) ); //</code> : This code is to get the ID list of users who edited the post and then merge the duplicate IDs together.</li>

   <li><code>foreach($results as $row){

$authors[] = get_the_author_meta('display_name', $row-&gt;post_author );

} //</code> : This loop will get the names of users corresponding to the IDs above.</li>

</ul>

<h2>Step 2: Show the Contributors List on the Post</h2>

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:

<pre>&lt;div class="list-contributors"&gt;

&lt;h3&gt;Contributors list&lt;/h3&gt;

        &lt;?php echo get_the_modified_authors(); ?&gt;

&lt;/div&gt;</pre>

<strong>Explanation:</strong>

<code>Contributors list</code> is the name of the list that will be displayed on the front end. You can give this contributor list any name you want.

<strong>Note</strong>: 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 <a href="https://gretathemes.com/wordpress-themes/estar/">here</a>), and I need to edit the <code>post.php</code> file.

Here is the contributors' list (with three users contributed to the post) on a single post page of my website:

<img class="aligncenter" src="https://i.imgur.com/YgRf2SR.png" alt="Show the Contributors List on the Post" width="919" height="541" />

If you want the contributors' list to be more beautiful and attractive, you can style it yourself. That's done! Easy peasy, right?

<h2>Last Words</h2>

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. If you follow this tutorial strictly, it will be so easy. Good luck and have fun building WordPress websites!

<hr />

The publication at <a href="https://metabox.io/create-contributor-list-for-post/">Meta Box</a>.