Minesh,
Thanks for your response. I figured it was completely outside the scope of Toolset support but wanted to start with you guys since you're WordPress wizards and might have some pointers—which you did! Thank you for sharing the links.
I ended up using none of those resources nor my snippet of code but did manage to find a solution on my own. My query was too complex; I joined the wp_postmeta table twice to wp_posts and needed to create table aliases. Just for posterity, in case someone else goes looking for something similar in the future, here is my solution.
The Problem. In the admin screen, I want to sort post type A based on custom fields in post type B. I had two custom post types: Artist and Artwork. They were connected via a one-to-many relationship. On the listing of Artworks, I wanted a sortable field name, "Artist," displaying the first name and last name (which are custom fields on the Artist post) of the artwork's artist in the format "last name, first name."
The Query. Below is the SQL query.
select artworks.post_title as artwork, concat(wpm1.meta_value,', ',wpm2.meta_value) as artist_name from wp_posts as artworks, wp_posts as artists, wp_toolset_associations as wta, wp_postmeta as wpm1, wp_postmeta as wpm2 where artworks.id = wta.child_id and artists.id = wta.parent_id and artists.id = wpm1.post_id and artists.id = wpm2.post_id and wpm1.meta_key='wpcf-last-name' and wpm2.meta_key='wpcf-first-name' and artworks.post_type='artwork' and artists.post_type='artist' order by wpm1.meta_value asc, wpm2.meta_value asc ;
Note: wpm1.meta_value is the surname, wpm2.meta_value is the first name.
The Solution. Because I needed to adjust several clauses in the query and change the name of the table I was selecting data from, I used the posts_clauses() hook to tweak the FIELDS, WHERE, and ORDER BY clauses, then I used the posts_request() hook to replace the table name, "artworks," with "wp_posts as artworks, wp_posts as artists, wp_toolset_associations as wta, wp_postmeta as wpm1, wp_postmeta as wpm2".
Caveat: don't forget to incorporate post_status in the posts_clauses() code! Otherwise filters won't work.
Hope this helps!
Saul