Multiple orderby parameters in pre_get_posts() action

Never forget that there’re actually two filters

// Add additional query args that are allowed by the API by default
pre_get_posts
// Modify the query herself
posts_clauses
// Inspect the resulting string - test this one in for e.g. phpMyAdmin
posts_request

So everything you can achieve using the pre_get_posts filter should be done there. The rest should be modified using the posts_clauses (or one of the more specific filters before).

// Modify the original query parts
function wpse70214_pre_get_posts( $query )
{
    var_dump( $query );
    return $query;
}
add_filter( 'pre_get_posts', 'wpse70214_pre_get_posts' );

// Modify the resulting query string parts
function wpse70214_posts_clauses( $pieces )
{
    var_dump( $pieces );
    return $pieces;
}
add_filter( 'posts_clauses', 'wpse70214_posts_clauses' );

// Then check the result
function wpse70214_posts_request( $query_string )
{
    var_dump( $query_string );
    return $query_string;
}
add_action( 'posts_request', 'wpse70214_posts_request' );

Leave a Comment