siteorigin / so-widgets-bundle

The SiteOrigin Widgets Bundle gives you all the elements you need to build modern, responsive, and engaging website pages. Using the Widgets Bundle, you can quickly and effortlessly add buttons, sliders, heroes, maps, images, carousels, features, icons, and so much more.
https://siteorigin.com/widgets-bundle/
94 stars 64 forks source link

WPML: Posts in widget's post search fields are not language-filtered #1423

Closed sitoexpress closed 2 years ago

sitoexpress commented 2 years ago

Hi there,

Just to let you know, I've noticed that posts displayed in any post-search field are not language-filtered when the website is multilingual and WPML is installed (cant say about polylang).

I noticed that the queries responsible for displaying posts are direct SQL queries (maybe because WP_Query isn't loaded at that time - same issue it happens by using get_terms() within a siteorigin widget).

Anyway, I wanted just to share a possible solution:

add_filter('siteorigin_widgets_search_posts_results',  'so_wpml_search_fix');
public function so_search_fix($results) {
  if(!defined('ICL_LANGUAGE_CODE')) return $results;
  $fixed = array();
  foreach($results as $result) {
      $code = apply_filters( 'wpml_post_language_details', '', $result['value'])['language_code'];
      if( $code == ICL_LANGUAGE_CODE) {
          $fixed[] = $result;
      }
  }
  return $fixed;
}

This works after the actual SQL query runs, however it's a useful workaround.

AlexGStapleton commented 2 years ago

Hi @frafor1988,

Sorry for the delay.

Thank you for reporting this. The provided snippet, while valid, will filter results rather than limit it to that specific language. This means that if the first page for that language is outside of the standard results (which is 20), it won't be listed. I've come up with a query to account for this, and submitted a PR that allows for this, and you can see it here: https://github.com/siteorigin/so-widgets-bundle/pull/1428

Kind regards, Alex

sitoexpress commented 2 years ago

Great!

I knew the limits of my workaround, however it was the only possible solution without altering the plugin code :)