Title: WordPress count post
Last modified: February 12, 2017

---

# WordPress count post

 *  [sonny12](https://wordpress.org/support/users/sonny12/)
 * (@sonny12)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/)
 * How can i count wordpress post. I am using custom post type UI and advanced custom
   field. I have created a post type called (product) and it has a fieldname called(
   product_family) which is a select tag and it has a 3 inside it (Latest,Car Navigation
   and Car Recorder).
 * I tried codes like this but cannot get it. How can i count the number inside 
   of the select tag.
 *     ```
        $args = array(
   
                                   'post_type'     => 'product',
                                   'post_per_page' => -1,
                                   'meta_key'      => 'product_family'
   
                                   );
   
                       $query = new WP_Query($args);
   
                       $count = $query->post_count;
   
                       echo '<p>' . $count . '</p>';
       ```
   
    -  This topic was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).

Viewing 12 replies - 1 through 12 (of 12 total)

 *  [Luis Sacristán](https://wordpress.org/support/users/displaynone/)
 * (@displaynone)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8796575)
 * Hi, you can use [wp_count_posts](https://codex.wordpress.org/Function_Reference/wp_count_posts)
 * `$count = wp_count_posts('product');`
 * I assume you don’t need to count posts with some meta value.
 * Hope it works
    -  This reply was modified 9 years, 4 months ago by [Luis Sacristán](https://wordpress.org/support/users/displaynone/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8797429)
 * If you want a count for each select value like “Latest”, you are on the right
   track with WP_Query, but you must provide a meta_value argument for the field
   value you are interested in. You’ll need to redo a query for each value.
 *  Thread Starter [sonny12](https://wordpress.org/support/users/sonny12/)
 * (@sonny12)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8797750)
 * [@displaynone](https://wordpress.org/support/users/displaynone/) and [@bcworkz](https://wordpress.org/support/users/bcworkz/).
   I wanted all the select tag value to be counted not the product mate. the fieldname
   for the select tag is (product_family) that is why i put meta_key for that but
   doesn’t do the trick.
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
 *  [1naveengiri](https://wordpress.org/support/users/1naveengiri/)
 * (@1naveengiri)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8797781)
 * Hi [@sonny12](https://wordpress.org/support/users/sonny12/),
    [@bcworkz](https://wordpress.org/support/users/bcworkz/)
   is right here you will have to pass meta_value to get posts by meta_key. you 
   could do it with meta_query we have in wp_query. try this code and if it works
   let me know.
 *     ```
       $args = array(
       	'post_type'     => 'product',
       	'post_per_page' => -1,
       	'meta_query' => array(
       		array(
       			'key'     => 'product_family',
       			'value'   => array( 'latest', 
       					'car_navigation', 
       					'thired_one'),
       			'compare' => 'IN',
       		),
       	),
       );
   
       $query = new WP_Query($args);
   
       $count = $query->post_count;
   
       echo '<p>' . $count . '</p>';
       ```
   
 *  Thread Starter [sonny12](https://wordpress.org/support/users/sonny12/)
 * (@sonny12)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8797813)
 * hi [@1naveengiri](https://wordpress.org/support/users/1naveengiri/)
    i tried 
   your code but the output is zero. And i want the select tag to be counted dynamically
   i cannot hardcode to put in array.
 * im using advanced custom field. i have post type name(product) and it has a fieldname(
   product_family) which is a select tag
    ⌊acf⌉
 * i specify both value and label. this is what i want to be counted dynamically
   so that in the future if the client will add another value, the code will automatic
   count it.
    ⌊acf2⌉
 * please shed some lights on how to do this. thanks,
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
 *  Thread Starter [sonny12](https://wordpress.org/support/users/sonny12/)
 * (@sonny12)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8797898)
 * im using advanced custom field. i have post type name(product) and it has a fieldname(
   product_family) which is a select tag
    ⌊acf⌉
 * i specify both value and label. this is what i want to be counted dynamically
   so that in the future if the client will add another value, the code will automatic
   count it.
    ⌊acf2⌉
 * hi [@1naveengiri](https://wordpress.org/support/users/1naveengiri/)
    i tried 
   your code but the output is zero. And i want the select tag to be counted dynamically
   i cannot hardcode to put in array.
 * please shed some lights on how to do this. thanks,
 *  [1naveengiri](https://wordpress.org/support/users/1naveengiri/)
 * (@1naveengiri)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8797978)
 * [@sonny12](https://wordpress.org/support/users/sonny12/),
    try this code and 
   give me output here.
 *     ```
       $field_key = "your_field_name";
       $field = get_field_object($field_key);
       ```
   
 * so that i could help you more.
 *  Thread Starter [sonny12](https://wordpress.org/support/users/sonny12/)
 * (@sonny12)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8798024)
 * [@1naveengiri](https://wordpress.org/support/users/1naveengiri/),
    the output
   in the browser is Array. ⌊browser⌉
 * below is the code you gave
 *     ```
       	       $args = array(
   
       		'post_type' 	=> 'product',
       		'post_per_page' => -1,
       		'meta_query'	=> array(
       										        array(
       											'key' => 'product_family',
       											'value' => array(
       													'latest',
       													'car_navigation',
       													'car_recorder'),			
       											    'compare' => 'IN',
       	)
           )
   
       );
   
       	$query = new WP_Query($args);
   
       	$field_key = "product_family";
       	$field = get_field_object($field_key);
   
       	echo $field;
       ```
   
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
    -  This reply was modified 9 years, 4 months ago by [sonny12](https://wordpress.org/support/users/sonny12/).
 *  [1naveengiri](https://wordpress.org/support/users/1naveengiri/)
 * (@1naveengiri)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8798055)
 * [@sonny12](https://wordpress.org/support/users/sonny12/) use var_dump($field);
   instead to echo it.
    -  This reply was modified 9 years, 4 months ago by [1naveengiri](https://wordpress.org/support/users/1naveengiri/).
 *  Thread Starter [sonny12](https://wordpress.org/support/users/sonny12/)
 * (@sonny12)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8798066)
 * var dump output
    ⌊acf⌉
 *  [1naveengiri](https://wordpress.org/support/users/1naveengiri/)
 * (@1naveengiri)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8798233)
 * [@sonny12](https://wordpress.org/support/users/sonny12/) the value seems false
   for your field here
    so please check whether you accessing right field. in value
   it should print all values you assign. and this array we will use to get value
   dynamically without hardcoding.
 *  [1naveengiri](https://wordpress.org/support/users/1naveengiri/)
 * (@1naveengiri)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8798239)
 * or try google to get all value of ACF dropdown field values.
    and use that to
   pass it dynamically in post meta_query.

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘WordPress count post’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 13 replies
 * 4 participants
 * Last reply from: [1naveengiri](https://wordpress.org/support/users/1naveengiri/)
 * Last activity: [9 years, 4 months ago](https://wordpress.org/support/topic/wordpress-count-post/#post-8798239)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
