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();