5

I'm trying to have a page where a user can view all of the custom post types after they click on an author from a previous page, but I'm having no luck with anything I can find in the built-in PHP functions with Wordpress.

Is this some easy to query? I haven't found much online about it.

2
  • Is it one specific post type, or do you mean "anything that isn't a WordPress core type"?
    – Pat J
    Jul 22, 2013 at 20:45
  • It's just one specific custom post type.
    – m0ngr31
    Jul 22, 2013 at 20:48

2 Answers 2

6

Something like this should work:

// Assuming you've got $author_id set
// and your post type is called 'your_post_type'
$args = array(
    'author' => $author_id,
    'post_type' => 'your_post_type',
);
$author_posts = new WP_Query( $args );
if( $author_posts->have_posts() ) {
    while( $author_posts->have_posts() ) { 
        $author_posts->the_post();
        // title, content, etc
        $author_posts->the_title();
        $author_posts->the_content();
        // you should have access to any of the tags you normally
        // can use in The Loop
    }
    wp_reset_postdata();
}

Reference

WP_Query class

Using an Author Template file

You can do this inside an Author Template:

author.php-- this file belongs in your theme's directory

<?php get_header(); ?>

<div id="content" class="narrowcolumn">

<!-- This sets the $curauth variable -->

<?php
    $curauth = (isset($_GET['author_name'])) ? 
        get_user_by('slug', $author_name) : 
        get_userdata(intval($author));
?>

<h2>About: <?php echo $curauth->nickname; ?></h2>
<dl>
    <dt>Website</dt>
    <dd><a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a></dd>
    <dt>Profile</dt>
    <dd><?php echo $curauth->user_description; ?></dd>
</dl>

<h2>Posts by <?php echo $curauth->nickname; ?>:</h2>

<ul>
<!-- The Loop -->
<?php

// Assuming your post type is called 'your_post_type'
$args = array(
    'author' => $curauth->ID,
    'post_type' => 'your_post_type',
);
$author_posts = new WP_Query( $args );
if( $author_posts->have_posts() ) {
    while( $author_posts->have_posts() ) {
        $author_posts->the_post();
        // title, content, etc
        the_title();
        the_content();
        // you should have access to any of the tags you normally
        // can use in The Loop
    }
    wp_reset_postdata();
}
?>

<!-- End Loop -->

</ul>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

This author.php template code is shamelessly cribbed from the Codex, and should probably be considered a starting point, not an end product.

10
  • I know I'll get some flack for this.. but I'm trying to do this with "Allow PHP in Post and Pages" and it seems to be having a problem with the if statement. I can't figure out why, but it just gives up trying to do anything as soon as it gets to that point.
    – m0ngr31
    Jul 22, 2013 at 21:26
  • The code I posted is mean to be added to your functions.php file or a plugin. I've got no experience using the Allow PHP in Posts and Pages plugin(?).
    – Pat J
    Jul 22, 2013 at 21:34
  • How would I add it to the functions.php? I'm not super great with PHP. I'm guessing I have to dump it in a function, but I'm not sure how to do the add_action stuff or how to call it from a page.
    – m0ngr31
    Jul 22, 2013 at 21:48
  • I've updated my response.
    – Pat J
    Jul 22, 2013 at 22:55
  • Hi there, late response, but I am trying your way for a user's posts in a CPT (events), but unfortunately, end up with a 500 server error on a user's page that actually has posted something - users without posts (i.e. empty query) work just fine. Can you have a look? See a paste of my author template file (overlook the crap with custom fields :) ) here: pastebin.com/NLNSYC0c . Thanks already!
    – physalis
    Sep 19, 2014 at 12:27
3

Use pre_get_posts to add your custom post type to your author.php template

In your functions.php, add the following code. This will add your custom post type to the main query so that it will appear on your author page

function wpse107459_add_cpt_author( $query ) {
    if ( !is_admin() && $query->is_author() && $query->is_main_query() ) {
        $query->set( 'post_type', array('post', 'YOUR_CUSTOM_POST_TYPE' ) );
    }
}
add_action( 'pre_get_posts', 'wpse107459_add_cpt_author' );

With this, there are no need to make any changes to your template files :-)

2
  • Hi @PieterGoosen This is almost perfect for what i need, but I would like to only show the CPT. On the $query->set line i could just remove 'post', but this will cause issues on the main blog. Do you know how i would set it so the code only applies to custom post types? In pseudo code it would be: if (post-type('cpt-name') { // do the code } but I can't seem to find a solution.
    – pjk_ok
    Jan 11, 2018 at 20:53
  • Thanks for this. Just solved my issue after trying 10 different other methods. Nov 7, 2018 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.