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 p

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.

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.

Share Improve this question asked Jul 22, 2013 at 20:38 m0ngr31m0ngr31 1511 gold badge1 silver badge2 bronze badges 2
  • Is it one specific post type, or do you mean "anything that isn't a WordPress core type"? – Pat J Commented Jul 22, 2013 at 20:45
  • It's just one specific custom post type. – m0ngr31 Commented Jul 22, 2013 at 20:48
Add a comment  | 

2 Answers 2

Reset to default 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.

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 :-)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742270785a4412658.html

相关推荐

  • 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 p

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信