Blog

Using Filters with Search Listings and How I Lost 24 Hours

By Matt Williams

Developers

The other day I ran into a problem with a non-Placester theme and Filters. 24 hours later I got to the solution after a firm slap on my forehead….grrrr, I hate it when that happens. To ensure it doesn’t happen to you, let me explain the whole thing. It’s all about Filters and Search Listings.

When using the Placester API in a wordpress theme, you can use two simple lines of code to get a search form and a search listings:

echo PLS_Partials::get_listings_search_form(
     'context=listings_search&ajax=1' );
echo PLS_Partials::get_listings_list_ajax(
     'context=listings_search&ajax=1&
     table_id=placester_listings_list' );

At first glance this is pretty magical. But you may think that its not all that flexible. I mean, get_listings_list_ajax just spits out a list in a generic looking format. Unless you just happened to choose the same CSS classes we did, this isn’t going to look all that great on your site. The trick to this is Filters.

PLS_Partials::get_listings_list_ajax is actually a two-step method: PLS_Partials_Get_Listings_Ajax::load() and PLS_Partials_Get_Listings_Ajax::get(). load() is what is called when you add PLS_Partials::get_listings_list_ajax to your php file. Its important to also include a small Javascript snippet which makes the call to the get() method. The JavaScript I added for the two lines above is:

    var search_bootloader;
    jQuery(document).ready(function( $ ) {
        search_bootloader = new SearchLoader ({
            filter: {
                context: 'listings_search'
            },
            list: {
                context: 'listings_search',
                limit_default: 8,
                limit_choices: [[10,20,50,-1], [10,20,50,"All"]]
            }
        });
    });

The get() method is what actually gets the list of listings and formats it in a nice way. If you take a look at the end of the get() definition you will see a filter is applied:

$item_html = apply_filters( 
    pls_get_merged_strings( 
        array("pls_listings_list_ajax_item_html", $context), 
        '_', 'pre', false ), htmlspecialchars_decode($item_html), 
        $listing, $context_var);

Since my context is listings_search, the filter I need to add is pls_listings_list_ajax_item_html_listings_search. To do this I added the following:

add_filter( 'pls_listings_list_ajax_item_html_listings_search',     
    array(__CLASS__,'listings_list_item', 10, 3 );</p>

public static function listings_list_item($html, $raw, $context_var) {
	ob_start();
	include 'template-listing-item.php';
	$html = ob_get_clean();
	$html = preg_replace('/[\n\r\t]/', ' ', $html);
	return $html;
}

The template-listing-item.php has the html I want to use for each item in the listing. Here is one line from that file:

MLS ID: <?php echo $raw['rets']['mls_id']; ?>

Using PHP object buffers, the raw fields get inserted into the html and injected into the $html variable, then returned. It’s really quite cool and using the template-listing-item.php file allows my code to be very clean.

Now what was the problem that sucked 24 hours of my life away??? I started this off as a quick test theme and included as much as possible in a single file. So in my index.php I had my add_filter, the method that it was calling, as well as the contents of the page. By moving the add_filter and the method to another file, in my case a listing class I created, everything magically began to work. Ugh, well at least that issue is complete…

Comments

Get our Newsletter
Follow Placester