0

I want to get number of images from blog page of wordpress..i have a code to get image nw but its giving me only images from individual post ..

Following is my code to get no of images in post ..

  $count_posts1 = wp_count_posts();
  $published_posts = $count_posts1->publish;
  echo "<br />number of post -----".$published_posts;
  $postimg = array();
  for ($j=0; $j<$published_posts; $j++)
  {
        $szPostContent = $post->post_content;
        $szSearchPattern = '#(<img.*?>)#';
        $a='';
        $aPics='';
        preg_match_all( $szSearchPattern, $szPostContent, $aPics );
        $iNumberOfPics = count($aPics[$j]);
        echo "<br />Number of pics on each post...............".$iNumberOfPics;

  }

I want to get count of total images in active/published posts. i could get no of published posts in blog and also no of images in single posts ..But how to integrate it in order to get all total count???

Vaibs_Cool
  • 6,126
  • 5
  • 28
  • 61
  • 1
    are you looking to check no of image in page or inside a post ..your title and query are different... – sAnS May 27 '13 at 08:58
  • @santanukumar I m trying to get count of images in a published posts... – Vaibs_Cool May 27 '13 at 09:08
  • @santanukumar Blog page means page on which all poss are shown.. – Vaibs_Cool May 27 '13 at 09:09
  • 1
    then why your title is `how to get no of images from blog page` it should be `how to get no of images within a wp post` – sAnS May 27 '13 at 09:14
  • i waanna get total image of all published post ...not of single post – Vaibs_Cool May 27 '13 at 09:32
  • @santanukumar --Did you get my question..I want no of count of images in published posts ..nd not of single post.. – Vaibs_Cool May 27 '13 at 10:18
  • can you try with this code... `$id=the_ID(); $total_attachments = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' && ID=$id"); echo $total_attachments;` – sAnS May 27 '13 at 10:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30680/discussion-between-user1145009-and-santanu-kumar) – Vaibs_Cool May 27 '13 at 10:22

1 Answers1

1

You may try like this

query_posts('post_status=publish'); 
while (have_posts()) : the_post(); 
$id = get_the_ID(); 
$total_attachments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND post_parent=$id"); 
echo $total_attachments; 
endwhile; 
wp_reset_query();
sAnS
  • 1,169
  • 1
  • 7
  • 10