Rasel Ahmed
Rasel Ahmed

Reputation: 305

Wordpress post query by author

I want a post list by author in page. I used author post will be dynamic in each page. I used the code below.

<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 3, 'post_type'=> 'post', 'author_name' => 'ppm' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>
</ul>

Please look on author_name array, I want post show for ppm user. Now I am going to do as dynamic. I want to get author name from page's custom field. Like

<?php $author_name = get_post_meta($post->ID, 'author_name', true); ?>
<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 3, 'post_type'=> 'post', 'author_name' => '$author_name' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>
</ul>

But, the code is not working. How can I get author name from page's custom field? Can anyone help?

Sorry for weak English.

Upvotes: 2

Views: 11895

Answers (2)

Rasel Ahmed
Rasel Ahmed

Reputation: 305

This is working for me. Thanks Sarim Khan for this solutions.

<ul>
<?php
global $post;
$author_name = get_post_meta($post->ID, 'author_name', true);
$args = array( 'posts_per_page' => 3, 'post_type'=> 'post', 'author_name' => $author_name );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>
</ul>

Upvotes: 2

Ravi Patel
Ravi Patel

Reputation: 5211

<?php while (have_posts() ) : the_post(); ?>
<?php $author_name = get_post_meta($post->ID, 'author_name', true); ?>
<?php endwhile; ?>


<?php
        $args = array( 'posts_per_page' => 3, 'post_type'=> 'post','meta_key'=>'author_name','meta_value'=>$author_name);
        query_posts($args);  
?>

<ul>
<?php while (have_posts() ) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>

Upvotes: 0

Related Questions