WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Display total number of images in blog built with wordpress ?

[Resolved] Display total number of images in blog built with wordpress ?

  • southcast
    Member

    @southcast

    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • esmi
    Forum Moderator

    @esmi

    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

    southcast
    Member

    @southcast

    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

    Prasath Mani
    Member

    @prasathpree

    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();
    ?>
    southcast
    Member

    @southcast

    @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.

    esmi
    Forum Moderator

    @esmi

    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.

    southcast
    Member

    @southcast

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

    Moderator keesiemeijer
    Moderator

    @keesiemeijer

    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;

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] Display total number of images in blog built with wordpress ?’ is closed to new replies.