Support

Account

Home Forums Front-end Issues Display the sum of specific field at front-end Reply To: Display the sum of specific field at front-end

  • Hi!

    I think I understand, you’re probably trying to query by a custom field.

    Looking at Example 1. from that resource page, it’s very similar to what you are trying to accomplish. Assuming your custom field is attached to some kind of query-able post type like posts or pages (my preference is to create a custom post type) this should be very easy.

    
    <?php 
    
    // args
    $args = array(
    	'posts_per_page' => -1, //get all posts
    	'post_type'	 => 'your_post_type_slug', //posts, pages, whatever the field is attached to
    	'meta_key'	 => 'listing-location', //custom field slug
    	'meta_value'	 => 'New York' //location to count
    );
    
    // query
    $the_query = new WP_Query( $args ); //get WP Query using $args
    
    $count = $the_query->post_count; //use property post_count to return the count
    
    echo 'Location: New York';
    echo 'Total listings I have with that location are '. $count; 
    ?>
    

    The property post_count is available when using a WP_Query. More information here: WP_Query

    Obviously this may not be the most efficient code because the location is hard coded, but maybe that works in your situation.

    My advice would be to set up the listing location as a custom category/taxonomy instead of an ACF field. That way you could loop through all the available categories dynamically.

    Hope this helps!