Getting a list of custom posts by author

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.

Related posts

2 comments

  1. 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.

  2. 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 🙂

Comments are closed.