0

Hi guys, someone know why this code is ignoring the author id and is giving to all logged user the same post, i want to show a single post related and published by logged user? all user has a post in that category and i want to show the post owned by logged user

$current_user = wp_get_current_user(); $current_user->ID; $post_ref = get_posts( array( 'author' => $current_user->ID, 'numberposts' => 1, 'cat' => 31 ) ); foreach( $post_ref as $post ): the_title(); echo get_the_ID(); endforeach;

1

why this code is ignoring the author id

It's not ignoring the author ID. But with that code, you need to call setup_postdata(), like this:

//global $post; // Uncomment if necessary.

$current_user = wp_get_current_user();
//$current_user->ID;

$post_ref = get_posts( array( 'author' => $current_user->ID, 'numberposts' => 1, 'cat' => 31 ) );
foreach( $post_ref as $post ):
  setup_postdata( $post ):
  the_title(); echo get_the_ID();
endforeach;
wp_reset_postdata();

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.