I have some code to sort search results by highest price or lowest price in Wordpress.
But I do not have a corresponding code for the form to make a selector for the code. Thank you
global $query_string;
if($_GET['orderby']=="highestprice") {
$args = array(
'post_type' => 'property',
'posts_per_page' => '-1',
'meta_key' => 'nt_listprice',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'nt_listprice'
)
)
);
} elseif($_GET['orderby']=="lowestprice") {
$args = array(
'post_type' => 'property',
'posts_per_page' => '-1',
'meta_key' => 'nt_listprice',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'nt_listprice'
)
)
);
} else {
$args = array(
'post_type' => 'property',
'posts_per_page' => '-1',
'order'=>'DESC',
'orderby'=> 'post_date'
);
}
There is some sample Form code here
<?php if ($nt_option["sp_location"] == "1"): ?>
<div class="col-lg-2 col-md-2 col-sm-12 col-xs-12">
<select class="form-control select" name="locations">
<option value=""><?php if (isset($nt_option["s_location"])) {
echo $nt_option["s_location"];
} ?></option>
<?php $locations = get_terms('location'); ?>
<?php foreach ($locations as $location): ?>
<option value="<?php echo $location->slug; ?>"><?php echo $location->name; ?></option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>