• Sprinto

    (@sprinto)


    Hey guys,

    Im having a problem with getting recent posts from a specific auther.

    I found a function that let be do it.

    function my_get_display_author_posts() {
        global $authordata, $post;
    
        $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ) ) );
    
        $output = '<ul>';
    
        $i = 0;
        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>';
            if(++$i > 4) break;
        }
        $output .= '</ul>';
    
        return $output;
    };

    But it only takes post 2,3,4,5 and not the most recent post.. Can anyone help me out? You can take a look at a post here: https://trendthis.dk/ny-bikini-trend-wtf/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator t-p

    (@t-p)

    Try this snippet:

    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;
    }
    Thread Starter Sprinto

    (@sprinto)

    still does not load the latest on ??

    Moderator bcworkz

    (@bcworkz)

    If the latest one is the current post, the ‘post__not_in’ argument is preventing it from loading.

    If that is not the case, something strange is going on. It’s common for queries to be completely wrong due to poor coding, but skipping over one post while otherwise correct is suspicious. Perhaps another plugin is confusing the process?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show posts from auther’ is closed to new replies.