How to use WP_Query with geodirectory
This topic contains 8 replies, has 3 voices, and was last updated by Paolo 10 years ago.
We have moved to a support ticketing system and our forums are now closed.
Open Support TicketTagged: WP_Query
-
AuthorPosts
-
January 17, 2016 at 3:50 pm #69111
I have found out how to get data using:
global $post, $wp_query, $preview, $geodirectory;
$zipcode = $post->post_zip;
or
$postid = $wp_query->post->ID;
$zipcode = geodir_get_post_meta($postid,’post_zip’,true);So, I can find out the post_zip of the CPT listing and I want to query how many listings/posts in the same post_zip are in the gd_places in that post_zip. This is inside an action where I am displaying data in the single listing of the CPT.
What is the way to to do that? Do I use WP_Query?
January 18, 2016 at 3:11 pm #69205As an example:
$args = array( 'post_type' => 'gd_place', 'post_zip' => '11949' ); $the_query = new WP_Query( $args ); echo $the_query->found_posts; wp_reset_postdata();This doesn’t filter any post_zip codes and gives me back the number of all posts in gd_place. I tried including a meta query:
'meta_query' => array( array( 'key' => 'gd_city', 'value' => 'manorville', 'compare' => '=' )(with zip code also) but it seems like these fields aren’t meta data??
hoping someone can help
January 18, 2016 at 4:30 pm #69222Hi,
I’ve flagged this for Stiofan, he’ll provide examples about this asap.
Thanks
January 18, 2016 at 4:54 pm #69225Hello,
Yes our fields are not standard meta data, we do this for many reasons.
We have a few helpfull functions if you already have an ID, https://wpgeodirectory.com/docs/codex/geodirectory_functions/geodir_get_post_info/
but if you want something like a query, this should do it:
global $wpdb, $plugin_prefix; $post_type = 'gd_place'; $table = $plugin_prefix . $post_type . '_detail'; $post_arr = $wpdb->get_results($wpdb->prepare( "SELECT * FROM $wpdb->posts p JOIN " . $table . " gd ON gd.post_id=p.ID WHERE gd.gd_city=%s ", array('manorville') ) , ARRAY_A);The good thing about this is you can filter by anything and multiple fields, if you have a complex query you want me to make a example of, just ask.
Thanks,
Stiofan
January 19, 2016 at 3:26 pm #69338Great, ok. Sounds good – I’ll play with this code and let you know if I’m having trouble getting the query to work.
Much Appreciated!
John
January 20, 2016 at 6:28 pm #69579Had to tweak the query to get it working – replace gd.gd_city with gd.post_city:
$post_arr = $wpdb->get_results($wpdb->prepare( "SELECT * FROM $wpdb->posts p JOIN " . $table . " gd ON gd.post_id=p.ID WHERE gd.post_city=%s ", array('Manorville') ) , ARRAY_A);May need a more complicated query but will give it a try before consulting you.
Thanks again!
January 20, 2016 at 6:31 pm #69580No probs, just let me know what u want and i’ll give an example 🙂
Stiofan
January 26, 2016 at 2:53 am #111478Follow up to this – is there any way to output the listings I query, in the format of the output of widgets and shortcodes, like gd_listings? Can I use any shortcodes or functions, or do I need to format my own custom html?
January 26, 2016 at 3:49 pm #111795You’ll have to use the listing-listview template to achieve that or recreate the same html structure.
Thanks
-
AuthorPosts
We have moved to a support ticketing system and our forums are now closed.
Open Support Ticket