Archived topic
Show custom posts by author
12 replies · Started by Kaleem on January 31, 2020
Hello Sir, I will like to display author's custom posts instead of showing all other posts by the same author. Please kindly help me achieve that.
Thank you for your wonderful work.
Hi there,
I'm not sure I understand - what do you mean by custom posts?
I’m trying to create a website and allow authors to submit two different posts (1. blog post 2. custom post type). So what I want to achieve is that I want to have two different links, one for displaying author "blog posts archive" and the other link for displaying "author post type archive" for the same author. So that when a user clicks on the "link 1" it only shows the blog posts for that author, and if the user clicks on "link 2" it shows the custom posts type for that same author.
Thank you so much.
Currently, the Author Posts Link shows both the blog posts and the custom post type of author.
But I want them separates.
Hmm, that might be tough.
We could try something like this:
add_action( 'pre_get_posts', function( $query ) {
if ( ! $query->is_main_query() || is_admin() ) {
return;
}
if ( is_author() ) {
$post_type = $_GET['post_type'];
if ( isset( $post_type ) ) {
$query->set( 'post_type', esc_attr( $post_type ) );
}
}
} );
Then you should be able to do this:
yoursite.com/author/username?post_type=your_custom_post_type
yoursite.com/author/username?post_type=post
I put the code in functions.php but it still couldn't work.
After a thorough search, I found the following similar to what I want: https://wordpress.org/support/topic/show-custom-post-type-archive-by-author/
Still, I couldn't do it.
Please have a look at the link, I strongly believe you will understand the code better.
Thanks a lot.
That code likely won't work in this case. Is my code still active on your site? If so, can you share your URL and the name of the custom post type?
Thank you so for your effort to help me solve this problem. In fact, I need that feature badly.
The site is in Coming Soon mode, so I have emailed you with the details.
Thanks a lot.
Note:
1. this url displays Page not found: yoursite.com/author/username?post_type=your_custom_post_type
2. this one displays normal author posts without filtering the Custom post type: yoursite.com/author/username?post_type=post
I'm not seeing any posts with the resolved post type assigned to that author, even when I view the standard author archives.
I'm not 100% sure my code will work, but it should in theory. If not, you may need to hire a developer to write a custom solution for you.
Ok, then I must crosscheck my codes very well.
Thank you very much for your effort.
No problem!
Hi, your solution was PERFECT! The whole problem caused by me due to improper creating of my CPT.
Thank you so much for this solution.
Awesome! Glad I could help :)