How do I extract just the post ID of the first item in whatever WP_Query returns? All the examples, answers, and documentation that I have seen dives off into doing things with loops. That's nice, but I just want the first ID. Nothing else. As the plugin will only ever generate a custom post type when there is none, the user should only have one. I need to get the ID of that post.
How do I find a post ID? Is there an easier way to find out if the user has a post available?
This is as far as I have gotten:
$query = new WP_Query( array(
'author' => $current_user,
'post_type' => 'my_custom_post_type',
// etc.
) );
$author_posts = new WP_Query( $query );
if( $author_posts->have_posts() ) {
// They have at least one. Grovey, now what?
}
unset($author_posts);
WP_Query::$posts
. E.g.$post_id = ( ! empty( $query->posts ) ) ? $query->posts[0]->ID : 0;
– Sally CJ Jul 7 '19 at 13:24