Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save badabingbreda/bd2f346c77f0f9d3dcf3cb45906a2a10 to your computer and use it in GitHub Desktop.
Save badabingbreda/bd2f346c77f0f9d3dcf3cb45906a2a10 to your computer and use it in GitHub Desktop.
toolbox - Timber Posts example query filter
<?php
/**
* pass in the query and make sure to return it at the end
*/
function my_query_filter( $query ) {
if (isset($_GET['bedrooms']) && $_GET['bedrooms'] !== '') {
$query[ 'meta_query' ][0]['relation'] = 'AND';
$query[ 'meta_query' ][0][] = array(
'key' => 'bedrooms',
'value' => $_GET['bedrooms'],
'compare' => 'LIKE'
);
}
if (isset($_GET['status'])&& $_GET['status'] !== '' ){
$query[ 'meta_query' ][0][] = array(
'key' => 'status',
'value' => $_GET['status'],
'compare' => 'LIKE'
);
}
if (isset($_GET['orderby']) && $_GET['orderby'] !== '' ) {
switch ($_GET['orderby']) {
case "priceasc":
$query[ 'orderby' ] = 'meta_value_num';
$query[ 'meta_key' ] = 'price';
$query[ 'order' ] = 'ASC';
break;
case "pricedesc":
$query[ 'orderby' ] = 'meta_value_num';
$query[ 'meta_key' ] = 'price';
$query[ 'order' ] = 'DESC';
break;
}
}
$query['cache_results'] = true;
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment