0

I try to show custom post list with WP_Query on author.php like this:

    <?php $args = array( 'post_type' => 'post', 'author' => 'get_queried_object_id()', 'posts_per_page' => 9 );
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        
<figure><a href="<?php the_permalink(); ?>"><img src="<?php if ( has_post_thumbnail() ) : the_post_thumbnail(); endif; ?>" alt="<?php the_title(); ?>"></a></figure>
<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>

                
        <?php endwhile; wp_reset_postdata(); ?>
        
        <?php else : ?>
        <p><?php _e( 'No posts by this author' ); ?></p>
    <?php endif; ?>

It gives posts from all authors for some reason. I've tried different combinations of 'author' array element with no luck or some other errors/issues (like displaying only one post for example).

Please help to solve this issue.

7
  • 1
    Try to remove single quotes here: 'get_queried_object_id()'
    – anton
    Commented Jun 6, 2021 at 16:09
  • anton, now it shows only one post by the actual author.
    – Alex
    Commented Jun 6, 2021 at 17:12
  • 1
    Are you sure you have more than 1 post per this author? Just checked your code on my wp, works fine and shows more than 1 post per author)
    – anton
    Commented Jun 6, 2021 at 17:31
  • 1
    Sorry, you're right - it works! It's my mistake with another author that has only one post... Thank you!
    – Alex
    Commented Jun 6, 2021 at 17:48
  • 1
    I will add it as an answer, if you do not mind. Let's remove this question from unanswered list)
    – anton
    Commented Jun 6, 2021 at 17:52

1 Answer 1

1

Functions in single quotes will be interpret as string.
You just need to remove single quotes in your $args array.

$args = array( 
    'post_type' => 'post', 
    'author' => get_queried_object_id(), //remove single quotes here
    'posts_per_page' => 9 
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.