0

I have the following select dropdown code for sorting on my search results page (search.php) that works for custom fields.

<div id="sortby"> SORT BY: &nbsp;
<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=DESC">Newest</option>
<option value="&orderby=date&order=ASC">Oldest</option>
<option value="&orderby=property_price2&order=DESC">Most Expensive</option>
<option value="&orderby=property_price2&order=ASC">Least Expensive</option>
<option value="&orderby=area2&order=DESC">Largest</option>
<option value="&orderby=area2&order=ASC">Smallest</option>
</select>
</div>

I also I have the following code in my functions.php to make this work --

function wpse139657_orderby(){
    if( isset($_GET['orderby']) ){
        $order = $_GET['order'] or 'DESC';
        set_query_var('orderby', 'meta_value_num');
        set_query_var('orderby', 'date');
        set_query_var('meta_type', 'numeric');
        set_query_var('meta_key', $_GET['orderby']);
        set_query_var('order', $order);
    }
}

add_filter('pre_get_posts','wpse139657_orderby');

It works when sorting by price or area, but it doesn't work when trying to sort by the date.

I've tried different meta_keys for the date ie - publish_date, the_date, date, get_the_date, etc..

What am I missing?

Also if use the sort on ie - the 3rd page, the results reload and keep me on the 3rd page, how can I bring the user to the 1st page of results after sorting?

1

The post date isn't meta. You only orderby meta_value if the value is a custom field stored in the wp_postmeta table. The publication date is in the wp_posts table and part of the core post data.

If you want to order by date you set orderby to date, and don't set any of the meta_ vars.

However, setting orderby and order with query strings is supported automatically, without pre_get_posts. For example, if you want to view Make WordPress Core's posts in reverse alphabetical order, you can just go to https://make.wordpress.org/core/?order=DESC&orderby=title and it'll work (no posts starting with Z yet!).

So all your function needs to do is handle the property_price2 and area2 orderby values, which WordPress won't understand on its own.

function wpse_139657_orderby() {    
    if ( isset( $_GET['orderby'] ) ) {
        if ( in_array( $_GET['orderby'], ['property_price2', 'area2'] ) ) {
            set_query_var( 'orderby', 'meta_value_num' );
            set_query_var( 'meta_key', $_GET['orderby'] );
        }
    }
}
add_filter( 'pre_get_posts', 'wpse_139657_orderby' );
| improve this answer | |
  • Thanks so far that seems to fix my first problem, I'm going to test some more to make sure. Any idea on how to go to the first page of the results after sorted? – Rich Mar 9 '18 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.