how do I get a count from a custom post post type that uses ACF?

I have a custom post type called "Property". In it I have some custom fields, built via the Advance Custom Fields plugin. One of the fields I built is a "select" field (pulldown) that has all 50 US states in there, the field name is 'state_prop'. What I need to do is have a list of each state and the quantity of how many posts each state is used in.

So if I have 100 total "Property" posts, how many are for Texas, how many for Illinois, etc. I can get a count for individual states by doing this:

$count = array('post_type' => 'Property', 'meta_key' => 'prop_state', 'meta_value' => 'Illinois');
$myquery = new WP_Query($count);
echo "<p>Found: $myquery->found_posts</p>";

But that's not very elegant because I would have to run that code 50 times, one for each state. I'm worried that would put too much strain on the server if it had to run all 50 time and time again.

I found some code that gets me closer with, but I don't know how to get the counts. I know I'll need to put all 50 states into the 'value', but how do I get them to echo out? The whole array inside an array thing baffles me:

$counteach = array( 'post_type' => 'Property', 'meta_query' =>
array( 'key' => 'prop_state', 'value' => 'Texas', 'Illinois'),

Is there a simpler way of doing what I want? Thanks for your help. When I signed up with WPMUDEV I remember reading something about "ask us any Wordpress question", so that's why I'm asking even though this has nothing to do with your plugins.

  • Rupok

    Hi Dave

    I hope you had a wonderful day.

    Yes, you can ask us any WordPress related question and we will be glad to help. However, I do agree with you that running a single type query 50 times doesn't sound a good idea, and I'm no expert in coding for suggesting you something better than the approach you are already taking.

    However, our SLS (Second Level Support) Team is highly expert in coding and I'm flagging them here so they can give you better solution for your query.

    Have a nice day. Cheers!

  • Dave

    It's almost three weeks since my initial question. The reason I signed up for your service is so I would have access to WordPress programmers that could help me with misc. questions. Your website says that you can answer any WP question, not just about your plugins. And my very first time you guys drop the ball, I haven't gotten a response from the SLS Team, let alone an answer to my question. Disappointing.

  • Kasia Swiderska

    Hello Dave,

    You asked in your first question "Is there a simpler way of doing what I want?" - so my question is - why not use custom taxonomies instead if custom meta?
    It will be easier to:
    a) list all taxonomies with posts attached to them
    b) and I believe counting those posts will be easier when you incorporate $post_count in your query.

    Here is sample code (I pulled it from older project of mine) - it lists all term in taxonomy (taxonomy is kind-of-offer in this case) and then lists posts that are assigned to each term:

    			$aArgs = array('hide_empty' => 0 );
    			$aOfferKinds = get_terms('kind-of-offer', $aArgs);
    			 if ( ! empty( $aOfferKinds ) && ! is_wp_error( $aOfferKinds ) ){
         echo ' <ul class="offer-list-icons">';
         foreach ( $aOfferKinds as $aOfferKind ) {
    	    $taxonomy =  $aOfferKind->taxonomy;
    		$term_id =  $aOfferKind->term_id;    
           echo '<li class="' . $aOfferKind->slug . '">';
           echo '<h2>' . $aOfferKind->name . '</h2>';
           			$aPostArgs = array('post_type' => 'offer', 'posts_per_page' => -1, 'orderby'   => 'menu_order', 'order'     => 'ASC',
           					'tax_query' => array(
    										'taxonomy' => 'kind-of-offer',
    										'field'    => 'id',
    										'terms'    =>  $aOfferKind->term_id,
           			$aOfferPost = get_posts( $aPostArgs );
           			if (! empty( $aOfferPost )) {
           		 echo '<ul>';
    foreach ( $aOfferPost as $post ) : setup_postdata( $post ); ?>
    	<li><?php the_title(); ?></li>
    <?php endforeach;
    echo '</ul>';
           echo '</li>';
       echo '</ul>';

    kind regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.