How to use WP_Query with geodirectory

This topic contains 8 replies, has 3 voices, and was last updated by  Paolo 8 years, 9 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket

Tagged: 

  • Author
    Posts
  • #69111

    macroclick
    Buyer
    Post count: 25

    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?

    #69205

    macroclick
    Buyer
    Post count: 25

    As 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

    #69222

    Paolo
    Site Admin
    Post count: 31206

    Hi,

    I’ve flagged this for Stiofan, he’ll provide examples about this asap.

    Thanks

    #69225

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Hello,

    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

    #69338

    macroclick
    Buyer
    Post count: 25

    Great, 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

    #69579

    macroclick
    Buyer
    Post count: 25

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

    #69580

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    No probs, just let me know what u want and i’ll give an example 🙂

    Stiofan

    #111478

    macroclick
    Buyer
    Post count: 25

    Follow 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?

    #111795

    Paolo
    Site Admin
    Post count: 31206

    You’ll have to use the listing-listview template to achieve that or recreate the same html structure.

    Thanks

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

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount