✨feature: Filtering working as intended
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
15
includes/template-loader.php
Normal file
15
includes/template-loader.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) { exit; } // Prevent direct access
|
||||
|
||||
function rfGetTemplate($template_name) {
|
||||
$theme_template = get_stylesheet_directory() . "/resource-filter/$template_name";
|
||||
$plugin_template = plugin_dir_path(__FILE__) . "../templates/$template_name";
|
||||
|
||||
if (file_exists($theme_template)) {
|
||||
return $theme_template;
|
||||
} elseif (file_exists($plugin_template)) {
|
||||
return $plugin_template;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
if (!defined('ABSPATH')) { exit; } // Prevent direct access
|
||||
|
||||
require_once plugin_dir_path(__FILE__) . 'includes/template-loader.php';
|
||||
|
||||
class ResourceFilterPlugin {
|
||||
public function __construct() {
|
||||
add_shortcode('resource_filter', [$this, 'renderFilterForm']);
|
||||
@@ -31,35 +33,20 @@ class ResourceFilterPlugin {
|
||||
|
||||
public function renderFilterForm() {
|
||||
ob_start();
|
||||
|
||||
$template = rfGetTemplate('filter-form.php');
|
||||
|
||||
if ($template) {
|
||||
include_once $template;
|
||||
} else {
|
||||
echo '<p>Error: Template not found.</p>';
|
||||
}
|
||||
|
||||
$total_resources = wp_count_posts('resource')->publish; // Get the count of published resources
|
||||
?>
|
||||
<form id="resource-filter">
|
||||
<input type="text" id="search" name="search" placeholder="Search resources...">
|
||||
|
||||
<?php
|
||||
$types = get_terms(['taxonomy' => 'resource_type', 'hide_empty' => true]);
|
||||
$subjects = get_terms(['taxonomy' => 'resource_subject', 'hide_empty' => true]);
|
||||
?>
|
||||
|
||||
<select id="resource_type" name="resource_type">
|
||||
<option value="">All Types</option>
|
||||
<?php foreach ($types as $type) : ?>
|
||||
<option value="<?php echo esc_attr($type->slug); ?>"><?php echo esc_html($type->name); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
|
||||
<select id="resource_subject" name="resource_subject">
|
||||
<option value="">All Subjects</option>
|
||||
<?php foreach ($subjects as $subject) : ?>
|
||||
<option value="<?php echo esc_attr($subject->slug); ?>"><?php echo esc_html($subject->name); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
|
||||
<button type="submit">Filter</button>
|
||||
</form>
|
||||
|
||||
<!-- Filter summary section -->
|
||||
<div id="resource-filter-summary">
|
||||
<strong>Showing <span id="result-count">0</span> resources</strong>
|
||||
<strong>Showing <span id="result-count"><?php echo $total_resources; ?></span> resources</strong>
|
||||
<p>Filters applied: <span id="applied-filters">None</span></p>
|
||||
</div>
|
||||
|
||||
@@ -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 '<div class="resource-grid grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">';
|
||||
while ($query->have_posts()) {
|
||||
$query->the_post();
|
||||
echo '<div class="resource-item"><a href="' . get_permalink() . '">' . get_the_title() . '</a></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
} else {
|
||||
echo '<p>No resources found.</p>';
|
||||
}
|
||||
$resources = $query->posts;
|
||||
$template = rfGetTemplate('resource-results.php');
|
||||
|
||||
wp_reset_postdata();
|
||||
if ($template) {
|
||||
include_once $template;
|
||||
} else {
|
||||
echo '<p>Error: Results template not found.</p>';
|
||||
}
|
||||
|
||||
// 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 '<div class="resource-grid grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">';
|
||||
while ($query->have_posts()) {
|
||||
$query->the_post();
|
||||
echo '<div class="resource-item"><a href="' . get_permalink() . '">' . get_the_title() . '</a></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
$query = new WP_Query($query_args);
|
||||
$resources = $query->posts;
|
||||
|
||||
$template = rfGetTemplate('resource-results.php');
|
||||
|
||||
if ($template) {
|
||||
include_once $template;
|
||||
} else {
|
||||
echo '<p>No resources found.</p>';
|
||||
echo '<p>Error: Results template not found.</p>';
|
||||
}
|
||||
|
||||
wp_reset_postdata();
|
||||
|
||||
26
templates/filter-form.php
Normal file
26
templates/filter-form.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) { exit; } // Prevent direct access
|
||||
|
||||
$resource_types = get_terms(['taxonomy' => 'resource_type', 'hide_empty' => true]);
|
||||
$resource_subjects = get_terms(['taxonomy' => 'resource_subject', 'hide_empty' => true]);
|
||||
?>
|
||||
|
||||
<form id="resource-filter">
|
||||
<input type="text" id="search" name="search" placeholder="Search resources...">
|
||||
|
||||
<select id="resource_type" name="resource_type">
|
||||
<option value="">All Types</option>
|
||||
<?php foreach ($resource_types as $type) : ?>
|
||||
<option value="<?php echo esc_attr($type->slug); ?>"><?php echo esc_html($type->name); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
|
||||
<select id="resource_subject" name="resource_subject">
|
||||
<option value="">All Subjects</option>
|
||||
<?php foreach ($resource_subjects as $subject) : ?>
|
||||
<option value="<?php echo esc_attr($subject->slug); ?>"><?php echo esc_html($subject->name); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
|
||||
<button type="submit">Filter</button>
|
||||
</form>
|
||||
15
templates/resource-results.php
Normal file
15
templates/resource-results.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) { exit; } // Prevent direct access
|
||||
|
||||
if (!empty($resources)) : ?>
|
||||
<?php
|
||||
foreach ($resources as $resource) :
|
||||
$post_id = $resource->ID;
|
||||
$post_title = get_the_title($post_id);
|
||||
$post_link = get_permalink($post_id);
|
||||
?>
|
||||
<div class="resource-item"><a href="<?php echo esc_url($post_link); ?>"><?php echo esc_html($post_title); ?></a></div>
|
||||
<?php endforeach; ?>
|
||||
<?php else : ?>
|
||||
<p>No resources found.</p>
|
||||
<?php endif; ?>
|
||||
Reference in New Issue
Block a user