WordPress.org

Forums

[resolved] Use wp_get_recent_posts or get_posts for certain author(s) only (2 posts)

  1. aalaap
    Member
    Posted 3 years ago #

    I'm building a custom template which requires access to recent posts, but only from one particular author (or a few specific authors). I saw that the wp_get_recent_posts and get_posts functions allow for filtering output by category, but there's no mention of being able to filter it by author(s).

    It does say on the wp_get_recent_posts function that it "uses WP_Query Parameters", which would mean it supports the "author=" parameter, but I'm not sure how to supply those to the function.

    I Googled a bit too, but no one seems to have asked this particular question. I've probably missed it...

    Thank you.

  2. aalaap
    Member
    Posted 3 years ago #

    Found this bit of code below that seems to demonstrate what I wanted:

    function get_related_author_posts() {
        global $authordata, $post;
    
        $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );
    
        $output = '<ul>';
        foreach ( $authors_posts as $authors_post ) {
            $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
        }
        $output .= '</ul>';
    
        return $output;
    }

    I'm gonna leave this thread here, so others can find it easily.

Topic Closed

This topic has been closed to new replies.

About this Topic