2

I have a task to find how many node are unpublished in a particular content type. Have to use the count for some other purposes.

How can I do that?

3 Answers 3

3

You just want to query the node table for example

here a sample function sent the content type machine name to this function will give you unpublished node count:

function get_unpublish_node_count($type){
            $db_query = db_select('node','n')
            -> fields('n',array('nid'))
            -> condition('n.status',0,'=')
            -> condition('n.type',$type,'=')
            $result = $db_query->countQuery()->execute()->fetchField();
            return $result;
}

Same function you can use this for published node count

function get_publish_node_count($type){
            $db_query = db_select('node','n')
            -> fields('n',array('nid'))
            -> condition('n.status',1,'=')
            -> condition('n.type',$type,'=')
            $result = $db_query->countQuery()->execute()->fetchField();
            return $result;
} 

just change the status to "1" will give you published node count

Hope this helps :)

0
1
$result = db_query("SELECT COUNT(*) FROM {node} WHERE type = :type AND status = 0", array(':type' => 'YOUR_NODE_TYPE'))->fetchField();
0

The Node Type count module do the same as you require.

This module is used to Show a count of nodes of a particular content-type and also number of Users of particular role type.

This module will be used for statistical and dev purposes only.

function node_type_count_published($content_type, $status = NODE_PUBLISHED) {
  $query = new EntityFieldQuery();
  // Grab nodes.
  $query->entityCondition('entity_type', 'node')
     // Filter by type.
    ->entityCondition('bundle', $content_type);

  if ($status) {
    // Filter by published.
    $query->propertyCondition('status', NODE_PUBLISHED);
  }
  else {
    // Filter by unpublished.
    $query->propertyCondition('status', NODE_NOT_PUBLISHED);
  }
  // Count.
  $query->count();
  $result = $query->execute();
  return $result;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.