Created
June 8, 2018 20:07
-
-
Save badabingbreda/bd2f346c77f0f9d3dcf3cb45906a2a10 to your computer and use it in GitHub Desktop.
toolbox - Timber Posts example query filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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