Custom query to get listings by Category

This topic contains 1 reply, has 2 voices, and was last updated by  Kiran 5 years, 5 months ago.

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

Open Support Ticket
  • Author
    Posts
  • #496239

    Dave
    Full Member
    Post count: 205

    How do I query places by category ID?

    I cannot use a conventional wp_query with category ID because the IDs are in a custom table, so presumably I would need to use a JOIN on the query.

    I’m assuming there is a function in GD to query listings in this manner, what is it and where can I find the syntax for using it?

    #496265

    Kiran
    Moderator
    Post count: 7069

    Hello David,

    You can retrieve listings by category id via API or via WP Query.

    Via API:
    http://YOURSITE.COM/wp-json/geodir/v2/places/?gd_placecategory=85

    Via WP_QUERY:

    
    
    $post_type 		= 'gd_place';		// Post type
    $category_id 	= array( 85 );		// Category ID
    
    $query_args 	= array(
    	'post_type' 		=> $post_type,
    	'post_status' 		=> array( 'publish' ),
    	'fields'			=> 'ids',
    	'posts_per_page'	=> 20, // -1 for all
    	'tax_query'			=> array(
    		array(
    			'taxonomy'         => $post_type . 'category',
    			'field'            => 'term_id',
    			'terms'            => $category_id,
    			'include_children' => true,
    		)
    	)
    );
    $posts_query 	= new WP_Query();
    $posts 			= $posts_query->query( $query_args );
    
    if ( ! empty( $posts ) ) {
    	foreach ( $posts as $post_ID ) {
    		$gd_post = geodir_get_post_info( $post_ID );
    	}
    }

    Let us know.
    Kiran

Viewing 2 posts - 1 through 2 (of 2 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