Custom Fields
Re-Envisioned
Support MB Relationships Filter the orderby of entries retrieved from database Reply To: Filter the orderby of entries retrieved from database

#11270

Clayton Collie
Participant

The function needs to be altered a little bit for anyone else looking to do the same thing. Only change was to remove ORDER BY in the new SQL statement.


add_filter( 'posts_clauses', 'mb_sort', 99, 2 );
/**
 * Sort just the POST relationship by date DESC.
 *
 * @param array $clauses Existing clauses.
 * @param WP_Query $query Main Query.
 * @return array
 */
function mb_sort( $clauses, $query ) {

    $relationship = $query->get( 'relationship' );
    
    if ( ! $relationship || 'REL_ID' !== $relationship['id'] ) {
            return $clauses;
    }
    
    $clauses['orderby'] = 'post_date DESC';
    
    return $clauses;
    
}