WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display total number of images in blog built with wordpress ? (8 posts)

  1. southcast
    Member
    Posted 1 year ago #

    I need some expert to suggest me a piece of code or a snippet in order to display total number of images ( all images within media library ). Please help. Thank you.

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    Try:

    $query_images_args = array(
        'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
    );
    $query_images = new WP_Query( $query_images_args );
    $images = array();
    $total_images = count($images);

    WARNING: UNTESTED

  3. southcast
    Member
    Posted 1 year ago #

    Thank you for the reply, but unfortunately it throws the following error. Is there a possible correction for this ?

    Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 24 bytes) in /home/xxxxxx/public_html/xxxxxxxxxxx/wp-includes/taxonomy.php on line 2657

  4. Prasath Mani
    Member
    Posted 1 year ago #

    Try

    Count total number of jpg, gif, png images in media library

    Adding the first snippet to the functions.php of your wordpress theme will display a count of all images within the media library. Add the second snippet in the location you wish to display the count total.

    function img_count(){
            $query_img_args = array(
                    'post_type' => 'attachment',
                    'post_mime_type' =>array(
                                    'jpg|jpeg|jpe' => 'image/jpeg',
                                    'gif' => 'image/gif',
                                    'png' => 'image/png',
                                    ),
                    'post_status' => 'inherit',
                    'posts_per_page' => -1,
                    );
            $query_img = new WP_Query( $query_img_args );
            echo $query_img->post_count;
    }
    <?
       img_count();
    ?>
  5. southcast
    Member
    Posted 1 year ago #

    @PrasathPree : Done as instructed and the frontend throws this error :

    Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 71 bytes) in /home/..../public_html/........./wp-includes/post.php on line 4431

    I run an image centric website built on wordpress and it is critical for me and my visitors to know the total number of images.

    Anyone with any corrections or suggestion please ! Thank You.

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    There isn't a solution that's going to avoid this issue. Quite simply, you're trying to process too much at one time. Try increasing the memory available to PHP.

  7. southcast
    Member
    Posted 1 year ago #

    Hmm...guess i will need to come up with an alternative. Neways @everybody I am grateful for the suggestions. Thank you.

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    Maybe try a direct sql query.

    global $wpdb;
    $images=  $wpdb->get_var("
      SELECT COUNT(ID) FROM $wpdb->posts
      WHERE (post_mime_type LIKE 'image/%')
      AND post_type = 'attachment' AND (post_status = 'inherit')
    ");
    echo 'total images= '. $images;

    For the total attached images:

    global $wpdb;
    $images=  $wpdb->get_var("
      SELECT COUNT(ID) FROM $wpdb->posts
      WHERE (post_mime_type LIKE 'image/%')
      AND post_type = 'attachment'
      AND (post_status = 'inherit')
      AND post_parent > 0
    ");
    echo 'total attached images= '. $images;

Topic Closed

This topic has been closed to new replies.

About this Topic