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?
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
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; ?>
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!
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!
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!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Merry Christmas and Happy Holidays WordPress Family 🎄🤗— Advanced Custom Fields (@wp_acf) December 24, 2020
© 2021 Advanced Custom Fields. Subscribe