Blog

Combining Multiple Lists of Listings Together

By Matt Williams

Developers

In my role at Placester, I often answer questions about Shortcodes. Shortcodes are amazingly powerful, allowing you to create totally customized search forms, lists of listings, slideshows and more. But I also love it when lower level questions come in. Today one of those questions came in front of me and I thought I would share it here.

The problem faced by this customer was that they needed to have a single page of all the listings offered by two different agents. These two agents worked in the same office, but they did not want to show all the listings of everyone else in that office, just these two agents.

Using Shortcodes this isn’t possible. Unfortunately its not even possible with the API in a single call. But it is possible to do two separate calls in the API and then merge the results together. So how do you do this?

Well PLS_Partials::get_listings() isn’t going to work since that returns the formatted HTML. You need to go one level deeper. PL_Listing::get() is what you want to look at. Once you run the two queries, you can merge the listings together into a single array. Then simply iterate through all the listings and generate the html according to your design criteria.

Here is the quick code sample I wrote for this particular customer.

function merge_listings(array $common_args, array $first_unique_args, array $second_unique_args) {
   $first = PL_Listing::get(array_merge($common_args,$first_unique_args));
   $second = PL_Listing::get(array_merge($common_args,$second_unique_args));
   $merged= array_merge($first["listings"], $second["listings"]);
   return $merged;
}

$mylistings = array();
$mylistings = merge_listings(array("context"=>"mycontext", "agency_only"=>true), array("rets[aid]"=>1234), array("rets[aid]"=>4567));
$listings_html = '';
foreach ($mylistings as $listing_data) {
   global $listings_html;
   $lhtml = '';
   $lhtml .= "<div>" .$listing_data['location']['address']." ".$listing_data['location']['locality'].", ".$listing_data['location']['region']."</div>";
   $lhtml .= "<div>".$listing_data['cur_data']['beds']." beds; ".$listing_data['cur_data']['baths']." baths</div><br>";

   $listings_html.=$lhtml;
}

echo $listings_html;

I hope you find this useful. If you have any questions you would like us to cover in this developer blog, let us know by adding a comment to the blog, or send me a tweet @technovangelist.

Related Articles

Popularity Contest: Why WordPress is Best
v0.3.9 Builder: Improved Messaging, Layout, and Options
News for our WordPress Plugin Users: International Support and Other Updates

Comments

Get our Newsletter
Follow Placester