On my search.php page, I have a "Sort By" dropdown that almost works exactly how I want it to --
<select class="dropdown-class" name="sort-posts" id="sortbox" onchange="document.location.href=location.href+this.options[this.selectedIndex].value;">
<option disabled>Sort by</option>
<option value="&orderby=date&order=dsc">Newest</option>
<option value="&orderby=date&order=asc">Oldest</option>
</select>
<script type="text/javascript">
<?php if (( $_GET['orderby'] == 'date') && ( $_GET['order'] == 'dsc')) { ?>
document.getElementById('sortbox').value='orderby=date&order=dsc';
<?php } elseif (( $_GET['orderby'] == 'date') && ( $_GET['order'] == 'asc')) { ?>
document.getElementById('sortbox').value='orderby=date&order=asc';
<?php } else { ?>
document.getElementById('sortbox').value='orderby=date&order=desc';
<?php } ?>
</script>
When Im on the search results page, the sort dropdown will sort the current results according to date by grabbing the url and appending to it then reloading the page with the results ---
// Before
mydomain.com/?s=Search&property_city=new-york-city&beds=Any
// After
mydomain.com/?s=Search&property_city=new-york-city&beds=Any&orderby=date&order=dsc
However, I am now trying to improve the code further by using it to sort based on numeric custom fields (high to low and low to high).
It seems all the info I can find on the subject have much more complicated ways of doing so. Is there anyway of doing this using the code I already started on?
UPDATE
I seem to be getting closer -
On my search.php page I have this before my loop ----
<select class="dropdown-class" name="sort-posts" id="sortbox" onchange="document.location.href=location.href+this.options[this.selectedIndex].value;">
<option disabled>Sort by</option>
<option value="&orderby=date&order=dsc">Newest</option>
<option value="&orderby=date&order=asc">Oldest</option>
<option value="&orderby=property_price&order=asc">Most Expensive</option>
<option value="&orderby=property_price&order=dsc">Least Expensive</option>
<option value="&orderby=property_area&order=dsc">Largest</option>
<option value="&orderby=property_area&order=asc">Smallest</option>
</select>
<script type="text/javascript">
<?php if (( $_GET['orderby'] == 'date') && ( $_GET['order'] == 'dsc')) { ?>
document.getElementById('sortbox').value='orderby=date&order=dsc';
<?php } elseif (( $_GET['orderby'] == 'date') && ( $_GET['order'] == 'asc')) { ?>
document.getElementById('sortbox').value='orderby=date&order=asc';
<?php } elseif (( $_GET['orderby'] == 'property_price') && ( $_GET['order'] == 'asc')) { ?>
document.getElementById('sortbox').value='orderby=property_price&order=asc';
<?php } elseif (( $_GET['orderby'] == 'property_price') && ( $_GET['order'] == 'dsc')) { ?>
document.getElementById('sortbox').value='orderby=property_price&order=dsc';
<?php } elseif (( $_GET['orderby'] == 'property_area') && ( $_GET['order'] == 'asc')) { ?>
document.getElementById('sortbox').value='orderby=property_area&order=asc';
<?php } elseif (( $_GET['orderby'] == 'property_area') && ( $_GET['order'] == 'dsc')) { ?>
document.getElementById('sortbox').value='orderby=property_area&order=dsc';
<?php } else { ?>
document.getElementById('sortbox').value='orderby=date&order=desc';
<?php } ?>
</script>
where it says "orderby=xxx" I just use my names for my custom fields.