How do I count the number of media attachments a specific post has?
Output example: This post has 22 photos.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.Sign up to join this community
My take would be this:
$posts = get_posts( array( 'post_parent' => $post, 'post_type' => 'attachment', 'fields' => 'ids', ) ); $count = count( $posts );
There is no need to query for all post fields if only intent is to count amount.
Also note that attachment is not necessarily image and that is not easy (possible?) to query for, see