3

I have large number of images in media library so I am accessing images in chunks via pagination in my Ruby on Rails application. I pass page number and offset to wp.getMediaLibrary API and it returns fixed number of images. So counting returned images is useless.

Here is my approach to get total number of images.

If we call wp.getMediaLibrary without passing number and offset, it will return all images and we can get count of images from results.

But the problem with this approach is that the site has huge number of images and so something goes wrong at server end and API return empty response.

Can anybody please guide me how to get count of images without getting all images information?

1

VERSION 1 will query all the images and give you a count by checking the size of the returned array. VERSION 2 is a much faster method introduced by birgire.

// VERSION 1

    $images = get_posts(array(
        'post_type'         => 'attachment',
        'post_status'       => 'any',
        'numberposts'       => -1,
        'fields'            => 'ids',
        'post_mime_type'    => 'image/jpeg,image/gif,image/jpg,image/png',
    ));

    echo count($images) . ' images total';

// VERSION 2

    $count = array_sum( (array) wp_count_attachments( 'image' ) );

    echo "{$count} images total";

ORIGINAL - For a full XML-RPC solution, create a custom method.

function xml_add_method( $methods ) {
    $methods['myNamespace.attachmentCount'] = 'get_attachment_count';
    return $methods;
}
add_filter( 'xmlrpc_methods', 'xml_add_method' );

function get_attachment_count( $args ) {
    // good to know it's here
    // global $wpdb; 

    // params passed in the call - not needed in this example
    $params = $args[3];

    // count the posts then return the total value
    $images = get_posts(array(
        'post_type'         => 'attachment',
        'post_status'       => 'any',
        'numberposts'       => -1,
        'fields'            => 'ids',
        'post_mime_type'    => 'image/jpeg,image/gif,image/jpg,image/png',
    ));

    // images total
    return count($images); 
}

Then do the RPC

global $current_user;

$user = $current_user->user_login;
$password = $user->data->user_pass;

include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
$xmlrpc_url = home_url('xmlrpc.php');
$client = new WP_HTTP_IXR_CLIENT( $xmlrpc_url );

// set this to true if you need help
// $client->debug = true;

$response = $client->query( 'myNamespace.attachmentCount', array(
    0,
    $user,
    $password,
    array(
        'post_type'         => 'attachment',
        'post_status'       => 'any',
    )
) );

if ( is_wp_error( $response ) ) {
    $error_message = $response->get_error_message();
    echo "Something went wrong: $error_message";
} else {
    echo 'Response:<pre>';        
    $count = $client->message->params[0]; // our return value is here
    print_r( $count . ' images total' );
    echo '</pre>';
}

UPDATE Merging @birgire's solution into this one.


add_filter('xmlrpc_methods', function ($methods) {
    $methods['myNamespace.getTotalImageCount'] = 'rpc_myNamespace_getTotalImageCount';
    return $methods;
});

function rpc_myNamespace_getTotalImageCount($args)
{
    return array_sum((array)wp_count_attachments('image'));
}

add_action('parse_request', function () {

    // PULL USER CREDS FROM CURRENT USER
    global $current_user;

    $user = $current_user->user_login;
    $password = $user->data->user_pass;

    include_once(ABSPATH . WPINC . '/class-IXR.php');
    include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
    $xmlrpc_url = home_url('xmlrpc.php');
    $client = new WP_HTTP_IXR_CLIENT($xmlrpc_url);

    // CALL OUR CUSTOM METHOD
    $response = $client->query('myNamespace.getTotalImageCount', array(0, $user, $password));

    echo 'Response:<pre>';
    $count = $client->message->params[0];
    print_r("{$count} total images");
    echo '</pre>';

    wp_die('FIN');
});
1
  • thanks for response. Let me clarify that I am accessing xml-rpc from Ruby on Rails application. Also I don't have control of the blog I am accessing. So I cannot write anything custom in wordpress blog.
    – Amit Patel
    Dec 17 '15 at 5:52
-3

I don't think WordPress has any functions to return just the count of images from Media Library.

I'm not familiar with XML RPC, but based on this post and this post, I think you can add custom methods.

If it works for you, then you can use the below query to get a count of images from Media Library:

global $wpdb;

$wpquery = $wpdb->get_results(
    "SELECT COUNT(*) FROM wp_posts WHERE post_type ='attachment' AND post_status = 'inherit'"
);
$count = mysql_fetch_array( $wpquery );

$wpdb->get_results( 
    "SELECT * FROM wp_posts WHERE post_type ='attachment' AND post_status = 'inherit'" 
);
$count = $wpdb->num_rows;
2
  • He is trying to use XML RPC to get the total count of a library on a remote server where he won't be adding any custom code. He'll never have access to $wpdb.
    – jgraup
    Dec 18 '15 at 20:04
  • Thanks Viren but as I said, I can mange blog via XML RPC only and cannot modify blog's script.
    – Amit Patel
    Dec 21 '15 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.