|
|
@@ -56,11 +56,6 @@ class Make_Sort_By extends WP_REST_Controller {
|
|
56
|
56
|
]
|
|
57
|
57
|
);
|
|
58
|
58
|
}
|
|
59
|
|
-
|
|
60
|
|
- if($params['episode']) {
|
|
61
|
|
- $args['p2p_query'] = $params['episode'];
|
|
62
|
|
- $args['tax_query'] = [];
|
|
63
|
|
- }
|
|
64
|
59
|
|
|
65
|
60
|
return $args;
|
|
66
|
61
|
}
|
|
|
@@ -139,11 +134,25 @@ class Make_Sort_By extends WP_REST_Controller {
|
|
139
|
134
|
|
|
140
|
135
|
public function by_episode( $request ) {
|
|
141
|
136
|
$q = new WP_Query($this->make_args($request, null));
|
|
142
|
|
- $episode_name = $q->query->p2p_query;
|
|
143
|
|
- $items = $this->prepare_items_for_reponse($q->get_posts());
|
|
|
137
|
+ global $wpdb;
|
|
|
138
|
+ $type = $q->query['post_type'] . 's';
|
|
|
139
|
+ // TODO: optimize this query
|
|
|
140
|
+ // $items = $q->get_posts();
|
|
|
141
|
+ $items = $wpdb->get_results($wpdb->prepare(
|
|
|
142
|
+ "SELECT DISTINCT wp_posts.*,
|
|
|
143
|
+ header.post_title AS related_episode
|
|
|
144
|
+ FROM wp_posts LEFT JOIN wp_p2p
|
|
|
145
|
+ ON (wp_p2p.p2p_from = wp_posts.ID OR wp_p2p.p2p_to = wp_posts.ID)
|
|
|
146
|
+ LEFT JOIN wp_posts AS header
|
|
|
147
|
+ ON header.ID = IF(wp_posts.ID = wp_p2p.p2p_to, wp_p2p.p2p_from, wp_p2p.p2p_to)
|
|
|
148
|
+ WHERE wp_posts.post_type = %s AND wp_posts.post_status = 'publish'
|
|
|
149
|
+ AND (wp_p2p.p2p_type = %s OR wp_p2p.p2p_type = %s)
|
|
|
150
|
+ ORDER BY header.post_date DESC, related_episode, wp_posts.post_date DESC",
|
|
|
151
|
+ trim($type, 's'), $type . "_to_episodes" , "episodes_to_" . $type,
|
|
|
152
|
+ ));
|
|
144
|
153
|
wp_reset_postdata();
|
|
145
|
154
|
|
|
146
|
|
- return new WP_REST_Response( $items, 200 );
|
|
|
155
|
+ return new WP_REST_Response( $this->prepare_items_for_reponse($items), 200 );
|
|
147
|
156
|
}
|
|
148
|
157
|
|
|
149
|
158
|
public function prepare_items_for_reponse( $items ) {
|
|
|
@@ -152,7 +161,6 @@ class Make_Sort_By extends WP_REST_Controller {
|
|
152
|
161
|
forEach( $items as $item ) {
|
|
153
|
162
|
$formatted = default_post_format( $item, false );
|
|
154
|
163
|
$formatted[hero] = get_post_meta( $item->ID, 'hero_header', true );
|
|
155
|
|
- $formatted[relatedto] = p2p_related_to( $item->ID, false );
|
|
156
|
164
|
array_push($collection, $formatted);
|
|
157
|
165
|
}
|
|
158
|
166
|
|