How to show a number count of photos attached to a classified ad.

I am trying to get my classifieds listing page to display a count of the pictures attached to each classified custom post type.

So far I have tried the following code inserted into the loop-taxonomy.php file in the classifieds plugin directory.

$id = get_the_ID();
$total_attachments = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' && ID=$id");
echo $total_attachments;

This seems to output "0" no matter how many pictures are attached to the post.

Any hints on the right direction to go from here would be great!

Also if someone knows how to limit the number of photo uploads allowed per classified post I am trying to find the answer to that as well.


  • caragammon
    • WPMU DEV Initiate

    Okay well now I feel silly for asking because I have found a solution.

    $attachments = get_children(array('post_parent'=>$post->ID));
    $nbImg = count($attachments);
    echo 'There are '.$nbImg.' pictures on this post.';

    This seems to accomplish everything I was looking for.

  • aecnu
    • WP Unicorn

    Greetings caragammon,

    Thank you for letting us know and for posting your solution for other members to benefit from which is certainly appreciated.

    In addition, thank you for being a WPMU DEV Community Member and have a GREAT upcoming weekend.

    Cheers, Joe

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.