• 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 7 years, 9 months ago by sonny12.
Viewing 12 replies - 1 through 12 (of 12 total)
  • Hi, you can use wp_count_posts

    $count = wp_count_posts('product');

    I assume you don’t need to count posts with some meta value.

    Hope it works

    Moderator bcworkz

    (@bcworkz)

    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

    (@sonny12)

    @displaynone and @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 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.

    Hi @sonny12,
    @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

    (@sonny12)

    hi @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 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.
    Thread Starter sonny12

    (@sonny12)

    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
    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,

    @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

    (@sonny12)

    @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 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.
    • This reply was modified 7 years, 9 months ago by sonny12.

    @sonny12 use var_dump($field); instead to echo it.

    • This reply was modified 7 years, 9 months ago by 1naveengiri.
    Thread Starter sonny12

    (@sonny12)

    var dump output
    acf

    @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.

    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.