WordPress XMLRPC: wp.getCommentCount

WordPress XMLRPC gives the following function wp.getCommentcount to retrieve comment count for a specific post.

Parameters for the function

  1. int blog_id
  2. string username
  3. string password
  4. string post_id

Return Values

  • array
    • struct
      1. int approved
      2. int awaiting_moderation
      3. int spam
      4. int total_comments

The return value gives the details of the comment like the number of comments approved for a particular posts, comments to be moderated, comments marked as spam

Let’s see the program. Note that you must fill the following parameters

  1. $USERNAME = “admin”;
  2. $PASSWORD = “check”;
  3. $BLOGID = 1;
  4. $BLOGURL = “http://localhost/wordpress”;
  5. $POSTID = 20;

Note that BLOGID is usually one or you can use wp.getUsersBlogs to get the value of BLOGID. POSTID is the ID of the post for which you want to find the comment count.


$USERNAME = "admin";
$PASSWORD = "check";
$BLOGID = 1;
$BLOGURL = "http://localhost/wordpress";
$POSTID = 20;

$XMLRPC_URL = $BLOGURL."/xmlrpc.php";

function get_response($URL, $context) {
 if(!function_exists('curl_init')) {
 die ("Curl PHP package not installedn");

 /*Initializing CURL*/
 $curlHandle = curl_init();

 /*The URL to be downloaded is set*/
 curl_setopt($curlHandle, CURLOPT_URL, $URL);
 curl_setopt($curlHandle, CURLOPT_HEADER, false);
 curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
 curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);

 /*Now execute the CURL, download the URL specified*/
 $response = curl_exec($curlHandle);
 return $response;

  /*Creating the wp.getCommentCount request*/
  $request = xmlrpc_encode_request("wp.getCommentCount",

  /*Making the request to wordpress XMLRPC of your blog*/
  $xmlresponse = get_response($XMLRPC_URL, $request);
  $response = xmlrpc_decode($xmlresponse);

  if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  } else {
    /*Printing the response on to the console*/
echo "n";

Let’s execute the program

$ php  wp.getCommentCount.php

The output will be something like this

<?xml version="1.0"?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s