Skip to content

Commit

Permalink
Fixes /popular-posts endpoint not translating posts (closes #284)
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrerahector committed Feb 16, 2021
1 parent 3fb2aba commit 831697e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Rest/PostsEndpoint.php
Expand Up @@ -36,6 +36,9 @@ public function get_items($request)
$lang = isset($params['lang']) ? $params['lang'] : null;
$popular_posts = [];

// Multilang support
$this->set_lang($lang);

$query = $this->maybe_query($params);
$results = $query->get_posts();

Expand All @@ -59,7 +62,15 @@ public function get_items($request)
*/
private function prepare_item($popular_post, $request)
{
$post_ID = $popular_post->id;
if ( $request->get_param('lang') ) {
$post_ID = $this->translate->get_object_id(
$popular_post->id,
get_post_type($popular_post->id)
);
} else {
$post_ID = $popular_post->id;
}

$wp_post = get_post($post_ID);

// Borrow prepare_item_for_response method from WP_REST_Posts_Controller.
Expand Down

0 comments on commit 831697e

Please sign in to comment.