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.

2 s
2

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.

Leave a Comment