DJP2019
DJP2019

Reputation: 69

Wordpress Get author Posts

Wonder if someone can help; its seems a bit complicated to me;

Baisically I've added this function in my wordpress site, so people can change the author's name;

add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );

function guest_author_name( $name ) {
global $post;

$author = get_post_meta( $post->ID, 'author_name', true );

if ( $author )
$name = $author;

return $name;
}  

But now I want to add some code to show a list of posts from the current posts author but its returning the original author not the author that the top function changed it to; below is the function I'm using to do this, is this possible to change?

function get_related_author_posts() {
    global $authordata, $post;
    $authors_posts = get_posts( array( 'author' => $author_name->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
    $output = '<div class="morepost"><h3>More posts from this author</h3>';
    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></div>';
    return $output;
}

then this;

<?php echo get_related_author_posts(); ?>

Know its a bit complicated, if anyone can help that be great

D

This is what Ive come up with so far but could someone show me where I'm going wrong with this code

<?php $author = get_post_meta( $post->ID, 'author_name', true ); $args
= array(
     'meta_query' => array (
           array(
              'key' => 'author_name',
            'value' => $author
        )
        ),    'post__not_in' => array( $post->ID ),   'posts_per_page' => 3 ); ?> <?php if ( $wp_query->have_posts() ) : ?> <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> DO something

<?php endwhile; ?> NOT POSTS <?php endif; ?>

Upvotes: 0

Views: 4536

Answers (1)

Rikesh
Rikesh

Reputation: 26431

You need to get post based on the author meta value,

So get the virtual author name first for that post,

$author = get_post_meta( $post->ID, 'author_name', true );

Now get all posts based on this value,

$args = array(
    'meta_query' => array(
        array(
            'key' => 'author_name',
            'value' => $author
        )
    ),
    'post__not_in' => array( $post->ID )
    'posts_per_page' => 3
);
$posts = get_posts($args);

Upvotes: 1

Related Questions