How to make an author archive only for certain user role and show related CPT

Scenario

I made a CPT where I’m taking the stakeholder’s ID as a custom field. All the stakeholders are actually a WordPress’ Registered User with a different role stakeholders – because at some point I need them to login to post their own content. So what actually I’m doing, I’m grabbing the userID and passing them as a custom field to that CPT.

Now I need to make a custom author archive, only specific to that role, where I can have all the posts of that CPT as per the custom field match.

What I did

I actually failed doing so. So I made a custom page template: where I queried all the users with the role stakeholders. They are visible now. But now I’m struggling showing the posts in which they are linked with their ID as a custom field.

Apart from this thought, I changed my route to another way, posting into that CPT using the certain user to activate their author archives. So, I tried, as an Admin, posting the post as a different user, but except the admin none other users are visible there in the dropdown menu. So I can’t choose other user for the post, while I’m logged in. 🙁
author dropdown menu

What can be

What I can imagine, or can do is: on my page template, on click pass the ID to the URL, grab it and show the posts specific to that ID on the same Page Template. But parameters like ?id=# is not SEO-friendly and I would need to rewrite URL – but I never tried that, so a bit afraid of it.

Question

What’s the correct approach to doing such a trick?

1 Answer
1

You can display user posts using user’s ID. This code might provide a quick sample on how to do it (I have not tested the code).

$user_id = $_GET['author_id'];
//The Query
query_posts("author={$user_id}");

//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
     the_title();
echo "<br>";
     endwhile;

else:
      echo "No Posts!";
endif;

//Reset Query
wp_reset_query();

As for the SEO part you can use many of the WordPress plugins that provide this functionality. Yoast SEO is a good plugin to do this.

Leave a Comment