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
ORDERBY
to every SQLSELECT
statement generated by theWP_Query
class. So it’s also very easy to break WordPress functionality using this filter.Fortunately, the
posts_orderby
filter will also pass a reference to the currentWP_Query
instance as a second parameter. It’s good practice to always check the referencedWP_Query
to ensure only the desired SQL query is being modified. This is especially true on a modern site, where in many cases more than oneWP_Query
is run per request.You can also prevent any
posts_orderby
callbacks from being loaded by using thesuppress_filters
in your ownWP_Query
calls:useful to custom rank posts on the front-end,