How to get listings ID in search page

Hi,

I might have found a way to insert my own customized map in the listings template (the one showing all the listings after performing a search).

But I would need a way to get all those search results (listings) in some form. What would be a code snippet to get all those listings (objects) so I can extract data from them? Similarly to $listing=\HivePress\Models\Listing::query()->get_by_id($listing_id); to get one listing object, there must be a way to get all listings from a search result.

And as a second question, when you know a listing ID, is there a way to get the listing object directly without having to perform the above query all the time?

Thank you!

Hi,

Please check how it’s implemented in the Geolocation map hivepress-geolocation/includes/blocks/class-listing-map.php at master · hivepress/hivepress-geolocation · GitHub There’s a WP query loop, and inside you can get the listing object with:

$listing=\HivePress\Models\Listing::query()->get_by_id(get_post());

This is the easiest way and using get_post() doesn’t make extra database queries, just converts the WordPress post object into the listing object, so you can access listing attributes like $listing->get_something();

Please also use rewind_posts(); at the end, otherwise the actual search results after the map may be affected.

Hope this helps

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.