admin管理员组文章数量:1315300
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 badges2 Answers
Reset to default 1If 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;
本文标签: pluginsHow to Loop Through all Posts and Count Attachments using Get Media Attachments
版权声明:本文标题:plugins - How to Loop Through all Posts and Count Attachments using Get Media Attachments 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741980456a2408366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论