Support

Account

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

Solving

Display the sum of specific field at front-end

  • Hi there,

    I’m hoping someone can help me find a solution for the following:

    I use ACF to create fields for a real estate site. Among the fields I’ve used is a field called ‘Listing-location’ which is a Text field.
    How can I show at front-end a sum of all listings with a specific location?

    Example: Location: New York
    Total listings I have with that location are 10.

    I use Elementor Pro and use it’s dynamic field feature to display my custom fields. I’m not savvy with PHP code to come up with something on my own and I’d like to display the sam of particular listings with specific locations in one of my sections at front-end.

    Any help please?

    Much appreciated..

  • 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!

  • Hi Jared,
    Thank you very much for taking the time to help me out!
    I appreciate your code too!
    In fact, I have created a taxonomy for ‘Listing-Location’ for my Custom post type, (created with CPT plugin).

    I know enough to change the slugs of your code but if you could kindly clarify, where would I place this PHP code so it displays in my section?
    On the taxonomy code, (provided within the CPT plugin interface)?
    That’s my trouble…not sure where to implement it!

    Thank you again and if it helps, I’m trying to achieve something very similar to the example

    at the section titled: ‘Explore Cities’ towards the end – they show for example Chicago: 13 properties.
    Well, I’m trying to get this dynamically to show for my locations!

    Thank you!!!

  • Hi Jared,

    I was wondering if you could help me out as well with a similar issue. I need to calculate the sum of an ACF number field in a CPT and show it in the front end with a shortcode… but I am not a programmer and don’t know how to write the code for it. I tried rewriting the code shown here – https://support.advancedcustomfields.com/forums/topic/calculating-a-sum-of-a-number-field-for-all-posts/ – with my own CPT and field slugs, etc, but it’s not working.

    The custom post type is ‘investment_project’ and the field is ‘dashboard_value’. How do I create a function that will add all the number values in the Dashboard value field and show it through a shortcode?

    All your help will be greatly appreciated!!!

    Thank you

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.