From 89eefba3c2334c4fbf7554fd637a00748cef4382 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Mon, 5 Apr 2021 14:27:38 +1000 Subject: [PATCH] Ignore categories that do not contain published posts Refs #539 --- components/Post.php | 2 +- models/Category.php | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/components/Post.php b/components/Post.php index b162897..8faa8ff 100644 --- a/components/Post.php +++ b/components/Post.php @@ -99,7 +99,7 @@ protected function loadPost() if (!$this->checkEditor()) { $query->isPublished(); } - + $post = $query->first(); /* diff --git a/models/Category.php b/models/Category.php index 80816bc..7f83355 100644 --- a/models/Category.php +++ b/models/Category.php @@ -255,8 +255,16 @@ public static function resolveMenuItem($item, $url, $theme) 'items' => [] ]; - $categories = self::orderBy('name')->get(); + $categories = self::with('posts_count')->orderBy('name')->get(); foreach ($categories as $category) { + try { + $postCount = $category->posts_count->first()->count ?? null; + if ($postCount === 0) { + continue; + } + } + catch (\Exception $ex) {} + $categoryItem = [ 'title' => $category->name, 'url' => self::getCategoryPageUrl($item->cmsPage, $category, $theme),