I'm building a slider that will display the latest post of a few selected authors. So far, I've written this:

<?php $args = array( 'post_type' => 'post', 'posts_per_page' => '1', 'author' => $user->ID ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); ?> <div class="author-box-title"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </div> <?php } } wp_reset_postdata(); ?>

and I don't get anything as a result. Can you tell me what's wrong with this? Also, is there a better way to generate the latest post by author?

Thanks everyone in advance.

  • That should work, granted that $user->ID contains a user id. – windyjonas Jan 20 '15 at 11:46
  • My guess would be that $user is undefined. How do you get $user. Do a var_dump($user) and make sure that you have a value – Pieter Goosen Jan 20 '15 at 12:01
  • The $user-ID is working as intended because I generate the images and the links to their profiles; but I still can't get their latest post to be displayed. – KM_ Jan 20 '15 at 12:49

What you can do is the following:


$where = get_posts_by_author_sql( 'post', true, $user->ID);

// get post query
global $wpdb;
$query = "SELECT ID FROM $wpdb->posts $where";
$post_ids = $wpdb->get_var( $wpdb->prepare( $query, 'Get all posts' ) );

Did not try it but something simular in your case should help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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