I need a plugin — or even just the right code! — to sort by custom fields.
Currently I have code on my category page that lets the user sort by title, newest, oldest and I want to add the ability to sort by a custom field of “rating”.
Here is the code I have now. Can you expand it to include custom fields?
<?php
$order = "&orderby=cost&order=DESC";
$s2 = ' selected="selected"';
if ($_POST['select'] == 'title') { $order = "&orderby=title&order=ASC"; $s1 = ' selected="selected"'; $s2 = ''; }
if ($_POST['select'] == 'newest') { $order = "&orderby=cost&order=DESC"; $s2 = ' selected="selected"'; }
if ($_POST['select'] == 'oldest') { $order = "&orderby=cost&order=ASC"; $s3 = ' selected="selected"'; $s2 = ''; }
?>
<form method="post" id="order">
Sort by:
<select name="select" onchange='this.form.submit()'>
<option value="title"<?=$s1?>>Title</option>
<option value="newest"<?=$s2?>>Newest</option>
<option value="oldest"<?=$s3?>>Oldest</option>
</select>
</form>