最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to Loop Through all Posts and Count Attachments using Get Media Attachments

programmeradmin0浏览0评论

I am working, on a code, where i need to loop Through all posts of a given author, and then for each post of that author, count the number of media attachments, the post has and then echo it.

I am able to loop Through posts, and it works as expected, but then i want to count the number of attachments of each post, where i fail, please help.

$author_posts = get_posts( array('author' => $author_id, 'numberposts' => -1, 'post_type'=> array('post','download', 'attachment' ), 'post_status'=> array('publish','privatised'), )); 

This was the orignal loop.

Below is the code, here the words are counted as expected, comments are counted as expected too. For simplicity in prefered not to include the echo portion

 foreach ( $author_posts as $post ) {   
$View_Count = absint( get_post_meta( $post->ID, 'Creation_Views', true ) ); 
$Word_Count = str_word_count( strip_tags( get_post_field( 'post_content', $post->ID )));
$Image_Count = count( get_attached_media( 'image', $post->ID ) );
$Comment_Count = get_comments_number($post->ID) ;

Please help, the $Image_Count, always gives 0, regardless of which post i check. I have over 500 posts, and it shows 0 for all of them, while all of them atleast have 1 attachment

I am working, on a code, where i need to loop Through all posts of a given author, and then for each post of that author, count the number of media attachments, the post has and then echo it.

I am able to loop Through posts, and it works as expected, but then i want to count the number of attachments of each post, where i fail, please help.

$author_posts = get_posts( array('author' => $author_id, 'numberposts' => -1, 'post_type'=> array('post','download', 'attachment' ), 'post_status'=> array('publish','privatised'), )); 

This was the orignal loop.

Below is the code, here the words are counted as expected, comments are counted as expected too. For simplicity in prefered not to include the echo portion

 foreach ( $author_posts as $post ) {   
$View_Count = absint( get_post_meta( $post->ID, 'Creation_Views', true ) ); 
$Word_Count = str_word_count( strip_tags( get_post_field( 'post_content', $post->ID )));
$Image_Count = count( get_attached_media( 'image', $post->ID ) );
$Comment_Count = get_comments_number($post->ID) ;

Please help, the $Image_Count, always gives 0, regardless of which post i check. I have over 500 posts, and it shows 0 for all of them, while all of them atleast have 1 attachment

Share Improve this question edited Feb 14, 2020 at 19:16 Aditya Agarwal asked Feb 14, 2020 at 18:03 Aditya AgarwalAditya Agarwal 2764 silver badges22 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

If you are counting post images, try the following. It doesn't capture a post's featured image, but it does get images in the post content.

$attached_images = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image;
$Image_Count=count($attached_images);

Must be used inside the loop

$post_images = get_children( $post->ID );
$images_count = count( $post_images );
echo $images_count; 
发布评论

评论列表(0)

  1. 暂无评论