WP REST API rest_post_query endpoint
-
Hello!
One of the Blocksy theme developers here.
Wanted to point out an important thing Polylang misses, in regard to querying posts using the
rest_post_query
WP REST API endpoint. And since Blocksy uses the WP REST API endpoints for fetching posts for its search live results feature, it’s retrieving the search results from all the languages, not just the current one.This is something that’s properly handled WPML, as far as I’ve tested. They properly pass the
lang
param into the WP_Query.Here’s a pretty hacky and unreliable way to fix this, using the referer URL. But I think everyone will benefit for a more solid solution implemented in the Polylang core.
add_filter( 'rest_post_query', function ($args, $request) { if (! isset($args['lang']) && function_exists('pll_default_language')) { $url = wp_get_referer(); $pattern = '#(' . implode( '|', pll_languages_list( array( 'fields' => 'slug' ) ) ) . ')#'; $language = preg_match( $pattern, trailingslashit( $url ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language if (! $language) { $language = pll_default_language(); } $args['lang'] = $language; } return $args; }, 10, 2 );
Thanks a lot for considering this!
Andrei
- The topic ‘WP REST API rest_post_query endpoint’ is closed to new replies.