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. |
Filters the ORDER BY clause of the query.
$orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
| 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,