From 362cece4a8d818920b0eab50e49e49936b24b4fd Mon Sep 17 00:00:00 2001 From: Ned Zimmerman Date: Mon, 30 Mar 2020 16:51:44 -0600 Subject: [PATCH 1/4] fix: language filtering (resolves #237) --- app/Controllers/App.php | 26 ++++++++++++------- app/filters.php | 5 ++-- .../views/partials/current-filters.blade.php | 4 +-- resources/views/partials/filters.blade.php | 2 +- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/app/Controllers/App.php b/app/Controllers/App.php index 078f455..e16567d 100644 --- a/app/Controllers/App.php +++ b/app/Controllers/App.php @@ -44,7 +44,7 @@ public function queriedResourceTerms() { $langs = get_language_list(pll_current_language('locale')); $terms = [ - 'language' => [], + 'resource_language' => [], 'lc_format' => [], 'lc_goal' => [], 'lc_region' => [], @@ -55,14 +55,16 @@ public function queriedResourceTerms() global $wp_query; if ($wp_query->tax_query) { foreach ($wp_query->tax_query->queries as $value) { - foreach ($value['terms'] as $t) { - $terms[$value['taxonomy']][$t] = get_term_by('slug', $t, $value['taxonomy'])->name; + if ($value['taxonomy'] !== 'language') { + foreach ($value['terms'] as $t) { + $terms[$value['taxonomy']][$t] = get_term_by('slug', $t, $value['taxonomy'])->name; + } } } } - if (isset($_GET['language'])) { - foreach ($_GET['language'] as $lang) { - $terms['language'][ $lang ] = $langs[$lang]; + if (isset($_GET['resource_language'])) { + foreach ($_GET['resource_language'] as $lang) { + $terms['resource_language'][ $lang ] = $langs[$lang]; } } return $terms; @@ -116,7 +118,7 @@ public function currentLanguage() return 'en'; } - public function foundPosts() + public function foundPosts($lang = 'en') { global $wp_query; return $wp_query->found_posts; @@ -374,10 +376,16 @@ public static function getPagination($args = []) return $navigation; } - public static function totalPosts($post_type = null) + public static function totalPosts($post_type = null, $lang = 'en') { if ($post_type) { - return wp_count_posts($post_type)->publish; + $q = new \WP_Query([ + 'post_type' => $post_type, + 'post_status' => 'publish', + 'lang' => $lang, + 'fields' => 'ids' + ]); + return $q->found_posts; } return 0; diff --git a/app/filters.php b/app/filters.php index ae0a564..d0c7e4a 100644 --- a/app/filters.php +++ b/app/filters.php @@ -136,11 +136,11 @@ break; } } - if (isset($_GET['language'])) { + if (isset($_GET['resource_language'])) { $meta_query = [ 'relation' => 'OR' ]; - foreach ($_GET['language'] as $lang) { + foreach ($_GET['resource_language'] as $lang) { $meta_query[] = [ 'key' => 'language', 'value' => $lang, @@ -150,7 +150,6 @@ } $query->set('posts_per_page', 12); $query->set('order', 'desc'); - $query->set('lang', ''); } } }); diff --git a/resources/views/partials/current-filters.blade.php b/resources/views/partials/current-filters.blade.php index cdb91ef..20aac69 100644 --- a/resources/views/partials/current-filters.blade.php +++ b/resources/views/partials/current-filters.blade.php @@ -1,6 +1,6 @@ -@if(isset($_GET['s']) || $found_posts < App::totalPosts('lc_resource')) +@if(isset($_GET['s']) || $found_posts < App::totalPosts('lc_resource', $current_language))
-

{{ sprintf(__('Showing %1$s of %2$s resources for:', 'coop-library'), $found_posts, App::totalPosts('lc_resource')) }}

+

{{ sprintf(__('Showing %1$s of %2$s resources for:', 'coop-library'), $found_posts, App::totalPosts('lc_resource', $current_language)) }}

@if(isset($_GET['s']))

{{ __('Search term', 'coop-library') }}

“{{ $_GET['s'] }}”

diff --git a/resources/views/partials/filters.blade.php b/resources/views/partials/filters.blade.php index 3eb87d6..7bb8e17 100644 --- a/resources/views/partials/filters.blade.php +++ b/resources/views/partials/filters.blade.php @@ -69,7 +69,7 @@