order posts by a secondary query that counts items
The way to go is apply custom filters to the WP_Query you are running. add_filter(‘posts_clauses’, function ($clauses, $query) { global $wpdb; // only run the filter for a given query // do your checks and return early if (!$query->is_main_query() || !$query->is_post_type_archive(‘tracklist’)) { return $clauses; } $clauses[‘fields’] .= “, COUNT(items.id) AS track_count”; $clauses[‘join’] .= ” LEFT … Read more