Filters the ORDER BY clause of the query.
Parameters
Source
$orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
Changelog
| Version | Description |
|---|---|
| 1.5.1 | Introduced. |
| CARVIEW |
Filters the ORDER BY clause of the query.
This filter is applied before a post-retrieving SQL statement is executed. Use it to make custom modifications to the orderby. WP_Query is versatile but there may be situations where you need to orderby a value that is in a separate database, a custom equation, etc.
$orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
| Used by | Description |
|---|---|
WP_Query::get_posts()wp-includes/class-wp-query.php | Retrieves an array of posts based on query variables. |
| Version | Description |
|---|---|
| 1.5.1 | Introduced. |
You must log in before being able to contribute a note or feedback.
Example migrated from Codex:
Consider the following code. In tandem with a rating plugin that uses a separate database table, sorting by rating value can be achieved with posts_orderby in tandem with the post_join_paged filter.
Example migrated from Codex:
This filter is extremely powerful – it will apply a new or updated
ORDERBYto every SQLSELECTstatement generated by theWP_Queryclass. So it’s also very easy to break WordPress functionality using this filter.Fortunately, the
posts_orderbyfilter will also pass a reference to the currentWP_Queryinstance as a second parameter. It’s good practice to always check the referencedWP_Queryto ensure only the desired SQL query is being modified. This is especially true on a modern site, where in many cases more than oneWP_Queryis run per request.You can also prevent any
posts_orderbycallbacks from being loaded by using thesuppress_filtersin your ownWP_Querycalls:useful to custom rank posts on the front-end,