diff --git a/README.md b/README.md index 9353354..85daf0a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# Resource Filter Plugin +# Content Filter Plugin ## Description -The Resource Filter Plugin adds filtering capabilities for the 'resource' post type by 'resource_type' and 'resource_subject'. This plugin provides a shortcode to display a filter form and dynamically updates the resource results based on the selected filters. +The Content Filter Plugin adds filtering capabilities for any registered post type by any taxonomies attched to it. This plugin provides a shortcode to display a filter form and dynamically updates the resource results based on the selected filters. ## Installation 1. Download the plugin files and place them in the `wp-content/plugins/resource-filter` directory. @@ -15,8 +15,13 @@ The Resource Filter Plugin adds filtering capabilities for the 'resource' post t To override the default form and results templates, create a `resource-filter` directory in your theme and copy the `filter-form.php` and `resource-results.php` files from the plugin `templates` directory to your theme directory. The plugin will use the template files in your theme directory instead of the default template files. ## Changelog -### 1.1.0 - 2025-02-04 +### 1.2.0 - 2025-02-05 +- Name change to reflect ability to filter more than 'resources' +- Add secondary search template + +### 1.1.0 - 2025-02-05 - Fully templated for use in any theme - Added result sorting + ### 1.0.0 - 2025-02-04 - Initial release diff --git a/resource-filter.php b/resource-filter.php index d6efb07..cd7bab6 100644 --- a/resource-filter.php +++ b/resource-filter.php @@ -1,8 +1,8 @@ 'default' // Accepts 'default' or 'homepage' + ], $atts, 'resource_filter'); + ob_start(); - $query = new WP_Query([ - 'post_type' => 'resource', - 'posts_per_page' => -1, - ]); + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $sort_order = isset($_POST['sort_order']) ? sanitize_text_field($_POST['sort_order']) : 'date_desc'; - $resTotal = $query->found_posts; // Get the count of published resources + $query_args = [ + 'post_type' => 'resource', + 'posts_per_page' => -1, + 'tax_query' => [], + 's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '', + ]; - $resForm = rfGetTemplate('filter-form.php'); + // Sorting logic + switch ($sort_order) { + case 'date_asc': + $query_args['orderby'] = 'date'; + $query_args['order'] = 'ASC'; + break; + + case 'date_desc': + $query_args['orderby'] = 'date'; + $query_args['order'] = 'DESC'; + break; + + case 'title_asc': + $query_args['orderby'] = 'title'; + $query_args['order'] = 'ASC'; + break; + + case 'title_desc': + $query_args['orderby'] = 'title'; + $query_args['order'] = 'DESC'; + break; + + default: + $query_args['orderby'] = 'date'; + $query_args['order'] = 'DESC'; + } + + $tax_query = []; + + if (!empty($_POST['resource_type'])) { + $resType = is_array($_POST['resource_type']) ? array_map('sanitize_text_field', $_POST['resource_type']) : sanitize_text_field($_POST['resource_type']); + + $query_args['tax_query'][] = [ + 'taxonomy' => 'resource_type', + 'field' => 'slug', + 'terms' => $resType, + 'operator' => 'IN' + ]; + } + + if (!empty($_POST['resource_subject'])) { + $query_args['tax_query'][] = [ + 'taxonomy' => 'resource_subject', + 'field' => 'slug', + 'terms' => array_map('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); + } else { + $query = new WP_Query([ + 'post_type' => 'resource', + 'posts_per_page' => -1, + ]); + } + + $resTotal = $query->found_posts; // Default total resource count + + // Determine which form template to load + $attTmpl = ($atts['type'] === 'homepage') ? 'filter-homepage.php' : 'filter-form.php'; + $resForm = rfGetTemplate($attTmpl); $summary = rfGetTemplate('filter-summary.php'); if ($resForm) { @@ -79,18 +154,26 @@ class ResourceFilterPlugin { echo '
Error: Form template not found.
'; } + if ($atts['type'] === 'default') { if ($summary) { include_once $summary; - }else { + } else { echo 'Error: Summary template not found.
'; } ?>Error: Results template not found.
'; } - // Prepare response JSON - $response = [ - 'count' => $query->found_posts, - 'filters' => [ - 'search' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '', - 'resource_type' => !empty($_POST['resource_type']) ? sanitize_text_field($_POST['resource_type']) : '', - 'resource_subject' => !empty($_POST['resource_subject']) ? sanitize_text_field($_POST['resource_subject']) : '' - ], - 'html' => ob_get_clean() - ]; + if ($is_ajax) { + // Prepare response JSON + $response = [ + 'count' => $query->found_posts, + 'filters' => [ + 'search' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '', + 'resource_type' => !empty($_POST['resource_type']) ? sanitize_text_field($_POST['resource_type']) : '', + 'resource_subject' => !empty($_POST['resource_subject']) ? sanitize_text_field($_POST['resource_subject']) : '' + ], + 'html' => ob_get_clean() + ]; - echo json_encode($response); - wp_die(); + echo json_encode($response); + wp_die(); + } else { + echo ob_get_clean(); + } } } diff --git a/templates/filter-homepage.php b/templates/filter-homepage.php new file mode 100644 index 0000000..5928de0 --- /dev/null +++ b/templates/filter-homepage.php @@ -0,0 +1,20 @@ + + +