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

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.