From c878c48e928f002257dd175f2f752a6e3c58d46e Mon Sep 17 00:00:00 2001
From: Keith Solomon
Date: Tue, 4 Feb 2025 13:44:36 -0600
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feature:=20Filtering=20working=20as=20?=
=?UTF-8?q?intended?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
assets/style.css | 43 ++++++++---
includes/template-loader.php | 15 ++++
resource-filter.php | 126 ++++++++++++++++-----------------
templates/filter-form.php | 26 +++++++
templates/resource-results.php | 15 ++++
5 files changed, 151 insertions(+), 74 deletions(-)
create mode 100644 includes/template-loader.php
create mode 100644 templates/filter-form.php
create mode 100644 templates/resource-results.php
diff --git a/assets/style.css b/assets/style.css
index d3446a4..fc2212f 100644
--- a/assets/style.css
+++ b/assets/style.css
@@ -1,13 +1,40 @@
#resource-filter {
display: flex;
- gap: 10px;
- margin-bottom: 20px;
+ gap: 1rem;
+ margin-bottom: 1.25rem;
+
+ input,
+ select,
+ button {
+ border: 1px solid #ccc;
+ font-size: 1rem;
+ padding: .5rem;
+ }
}
-#resource-filter input,
-#resource-filter select,
-#resource-filter button {
- border: 1px solid #ccc;
- font-size: 16px;
- padding: 8px;
+#resource-filter-summary {
+ display: flex;
+ gap: .5rem;
+ margin-bottom: 1.25rem;
+
+ p { margin: 0; }
+}
+
+#resource-results {
+ display: grid;
+ grid-template-columns: repeat(1, minmax(0, 1fr));
+ gap: 1rem;
+}
+
+
+@media (min-width: 768px) { /* Tailwind 'md' breakpoint */
+ #resource-results {
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ }
+}
+
+@media (min-width: 1024px) { /* Tailwind 'lg' breakpoint */
+ #resource-results {
+ grid-template-columns: repeat(4, minmax(0, 1fr));
+ }
}
diff --git a/includes/template-loader.php b/includes/template-loader.php
new file mode 100644
index 0000000..203fa56
--- /dev/null
+++ b/includes/template-loader.php
@@ -0,0 +1,15 @@
+Error: Template not found.
';
+ }
+
+ $total_resources = wp_count_posts('resource')->publish; // Get the count of published resources
?>
-
-
-
-
Showing 0 resources
+
Showing resources
Filters applied: None
@@ -75,47 +62,51 @@ class ResourceFilterPlugin {
check_ajax_referer('resource_filter_nonce', 'nonce');
$query_args = [
- 'post_type' => 'resource',
+ 'post_type' => 'resource',
'posts_per_page' => -1,
- 'tax_query' => [],
- 's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
+ 'tax_query' => [],
+ 's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
];
- if (!empty($_POST['resource_type']) || !empty($_POST['resource_subject'])) {
- $query_args['tax_query']['relation'] = 'AND';
+ $tax_query = [];
- if (!empty($_POST['resource_type'])) {
- $query_args['tax_query'][] = [
- 'taxonomy' => 'resource_type',
- 'field' => 'slug',
- 'terms' => sanitize_text_field($_POST['resource_type'])
- ];
- }
+ if (!empty($_POST['resource_type'])) {
+ $tax_query[] = [
+ 'taxonomy' => 'resource_type',
+ 'field' => 'slug',
+ 'terms' => [sanitize_text_field($_POST['resource_type'])], // Ensure it's an array
+ 'operator' => 'IN'
+ ];
+ }
- if (!empty($_POST['resource_subject'])) {
- $query_args['tax_query'][] = [
- 'taxonomy' => 'resource_subject',
- 'field' => 'slug',
- 'terms' => sanitize_text_field($_POST['resource_subject'])
- ];
- }
+ if (!empty($_POST['resource_subject'])) {
+ $tax_query[] = [
+ 'taxonomy' => 'resource_subject',
+ 'field' => 'slug',
+ 'terms' => [sanitize_text_field($_POST['resource_subject'])],
+ 'operator' => 'IN'
+ ];
+ }
+
+ if (!empty($tax_query)) {
+ $query_args['tax_query'] = [
+ 'relation' => 'AND', // Both filters must match
+ ...$tax_query
+ ];
}
$query = new WP_Query($query_args);
+
ob_start();
- if ($query->have_posts()) {
- echo '';
- while ($query->have_posts()) {
- $query->the_post();
- echo '
';
- }
- echo '
';
- } else {
- echo 'No resources found.
';
- }
+ $resources = $query->posts;
+ $template = rfGetTemplate('resource-results.php');
- wp_reset_postdata();
+ if ($template) {
+ include_once $template;
+ } else {
+ echo 'Error: Results template not found.
';
+ }
// Prepare response JSON
$response = [
@@ -132,18 +123,21 @@ class ResourceFilterPlugin {
wp_die();
}
- private function loadResources($query_args = ['post_type' => 'resource', 'posts_per_page' => -1]) {
- $query = new WP_Query($query_args);
+ public function loadResources() {
+ $query_args = [
+ 'post_type' => 'resource',
+ 'posts_per_page' => -1
+ ];
- if ($query->have_posts()) {
- echo '';
- while ($query->have_posts()) {
- $query->the_post();
- echo '
';
- }
- echo '
';
+ $query = new WP_Query($query_args);
+ $resources = $query->posts;
+
+ $template = rfGetTemplate('resource-results.php');
+
+ if ($template) {
+ include_once $template;
} else {
- echo 'No resources found.
';
+ echo 'Error: Results template not found.
';
}
wp_reset_postdata();
diff --git a/templates/filter-form.php b/templates/filter-form.php
new file mode 100644
index 0000000..1e5efd5
--- /dev/null
+++ b/templates/filter-form.php
@@ -0,0 +1,26 @@
+ 'resource_type', 'hide_empty' => true]);
+$resource_subjects = get_terms(['taxonomy' => 'resource_subject', 'hide_empty' => true]);
+?>
+
+
diff --git a/templates/resource-results.php b/templates/resource-results.php
new file mode 100644
index 0000000..e5f4e11
--- /dev/null
+++ b/templates/resource-results.php
@@ -0,0 +1,15 @@
+
+ ID;
+ $post_title = get_the_title($post_id);
+ $post_link = get_permalink($post_id);
+ ?>
+
+
+
+ No resources found.
+