This works. The filters are tied to a custom 'orderby'
term and removed after the query.
$args = array(
'post_type' => 'artworks',
'posts_per_page' => -1,
'no_found_rows' => true,
'order' => 'ASC',
'orderby' => 'artist_name_artwork_title',
);
add_filter('posts_join', 'name_join', 10, 2 );
add_filter('posts_orderby', 'orderby_artist_name_artwork_title', 10, 2);
// query
$wp_query = new WP_Query( $args );
remove_filter('posts_join', 'name_join', 10 );
remove_filter('posts_orderby', 'orderby_artist_name_artwork_title', 10 );
function name_join($joins, $wp_query) {
if ( $wp_query->get( 'orderby' ) != 'artist_name_artwork_title' ) {
return $joins;
}
global $wpdb;
$joins .= " LEFT JOIN $wpdb->postmeta name ON name.post_id=$wpdb->posts.ID AND name.meta_key='artists_name'" ;
$joins .= " LEFT JOIN $wpdb->postmeta lastname ON lastname.post_id=name.meta_value AND lastname.meta_key='artist_last_name'";
$joins .= " LEFT JOIN $wpdb->postmeta firstname ON firstname.post_id=name.meta_value AND firstname.meta_key='artist_first_name'";
return $joins;
}
function orderby_artist_name_artwork_title($orderby_statement, $wp_query) {
if ( $wp_query->get( 'orderby' ) != 'artist_name_artwork_title' ) {
return $orderby_statement;
}
global $wpdb;
$orderby_statement = "lastname.meta_value, firstname.meta_value, $wpdb->posts.post_title";
return $orderby_statement;
}