Created
December 11, 2024 18:19
-
-
Save hmowais/2a3ef81d063ae5f12c8d79b182e034d6 to your computer and use it in GitHub Desktop.
Custom Filters CPT + Taxonomies [Shortcode]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Litigation Filters */ | |
function multi_filter_litigation_cases_shortcode($atts) { | |
// Get filter values from URL parameters or defaults | |
$state = isset($_GET['state']) ? sanitize_text_field($_GET['state']) : ''; | |
$issue = isset($_GET['issue']) ? sanitize_text_field($_GET['issue']) : ''; | |
$case_status = isset($_GET['case_status']) ? sanitize_text_field($_GET['case_status']) : ''; | |
// Set the query arguments | |
$args = array( | |
'post_type' => 'litigation', | |
'posts_per_page' => -1, // Show all cases | |
'orderby' => 'title', | |
'order' => 'ASC', | |
); | |
// Initialize tax_query for multiple taxonomies | |
$tax_query = array('relation' => 'AND'); // Ensure multiple filters work together | |
// Add State filter if specified | |
if ($state) { | |
$tax_query[] = array( | |
'taxonomy' => 'litigation_states', | |
'field' => 'slug', | |
'terms' => $state, | |
); | |
} | |
// Add Voting Issue filter if specified | |
if ($issue) { | |
$tax_query[] = array( | |
'taxonomy' => 'litigation_categories', | |
'field' => 'slug', | |
'terms' => $issue, | |
); | |
} | |
// Add Litigation Case status filter if specified | |
if ($case_status) { | |
$tax_query[] = array( | |
'taxonomy' => 'litigation_cases', | |
'field' => 'slug', | |
'terms' => $case_status, | |
); | |
} | |
// If there are any tax queries, add them to the main query | |
if (count($tax_query) > 1) { // Means there are filters applied | |
$args['tax_query'] = $tax_query; | |
} | |
// Query the litigation cases | |
$query = new WP_Query($args); | |
// Start output buffer | |
ob_start(); | |
// Filter form for users to select their filters | |
?> | |
<form method="get" action="" class="litigation_form" style="display: flex; justify-content: space-between; align-items: center;"> | |
<div class="litigation_search" style="flex: 1; display: flex; justify-content: flex-start; gap: 20px;"> | |
<label for="state" style="width: 300px;"> | |
Select State: | |
<select name="state" id="state" class="litigation-select"> | |
<option value="">All States</option> | |
<?php | |
$states = get_terms(array( | |
'taxonomy' => 'litigation_states', // Taxonomy for states | |
'orderby' => 'name', // Sort by name (A-Z) | |
'order' => 'ASC', // Ascending order | |
'hide_empty' => false | |
)); | |
foreach ($states as $state_term) { | |
echo '<option value="' . $state_term->slug . '" ' . selected($state, $state_term->slug, false) . '>' . $state_term->name . '</option>'; | |
} | |
?> | |
</select> | |
</label> | |
<label for="issue" style="width: 400px;"> | |
Select Voting Issue: | |
<select name="issue" id="issue" class="litigation-select"> | |
<option value="">All Voting Issues</option> | |
<?php | |
$categories = get_terms(array( | |
'taxonomy' => 'litigation_categories', | |
'orderby' => 'name', | |
'order' => 'ASC', | |
'hide_empty' => false | |
)); | |
foreach ($categories as $category) { | |
echo '<option value="' . $category->slug . '" ' . selected($issue, $category->slug, false) . '>' . $category->name . '</option>'; | |
} | |
?> | |
</select> | |
</label> | |
<label for="case_status" style="width: 300px;"> | |
Filter by Case Status: | |
<select name="case_status" id="case_status" class="litigation-select"> | |
<option value="">All Cases</option> | |
<?php | |
$cases = get_terms(array( | |
'taxonomy' => 'litigation_cases', | |
'orderby' => 'name', | |
'order' => 'ASC', | |
'hide_empty' => false | |
)); | |
foreach ($cases as $case) { | |
echo '<option value="' . $case->slug . '" ' . selected($case_status, $case->slug, false) . '>' . $case->name . '</option>'; | |
} | |
?> | |
</select> | |
</label> | |
</div> | |
<div class="litigation_button" style="flex: 1; display: flex; justify-content: flex-end; align-items: center; gap: 20px;"> | |
<input type="submit" value="Filter" class="filter_litigation"> | |
<a href="<?php echo esc_url(remove_query_arg(array('state', 'issue', 'case_status'))); ?>" class="reset_litigations">Reset Filters</a> | |
</div> | |
</form> | |
<?php | |
// Display the filtered cases in a grid | |
if ($query->have_posts()) { | |
echo '<div class="litigation-grid">'; // Start the grid container | |
// Get states in alphabetical order | |
$states_ordered = get_terms(array( | |
'taxonomy' => 'litigation_states', | |
'orderby' => 'name', | |
'order' => 'ASC', | |
'hide_empty' => false | |
)); | |
// Loop through states in alphabetical order | |
foreach ($states_ordered as $state_term) { | |
//echo '<h2>' . $state_term->name . '</h2>'; // Display state name | |
// Query posts for each state | |
$state_posts = new WP_Query(array( | |
'post_type' => 'litigation', | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'litigation_states', | |
'field' => 'slug', | |
'terms' => $state_term->slug | |
) | |
) | |
)); | |
// Check if there are posts for this state | |
if ($state_posts->have_posts()) { | |
while ($state_posts->have_posts()) { | |
$state_posts->the_post(); | |
// Display individual post | |
echo '<div class="litigation_post">'; | |
echo '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>'; | |
echo '<p>' . wp_trim_words(get_the_excerpt(), 20, '...') . '</p>'; | |
echo '<a href="' . get_permalink() . '">Read More</a>'; | |
echo '</div>'; // End post | |
} | |
} else { | |
echo '<p>No cases available for ' . $state_term->name . '</p>'; | |
} | |
wp_reset_postdata(); // Reset post data after each state query | |
} | |
echo '</div>'; // End grid container | |
} else { | |
echo 'No cases found matching the selected filters.'; | |
} | |
wp_reset_postdata(); | |
return ob_get_clean(); | |
} | |
add_shortcode('multi_filter_litigation', 'multi_filter_litigation_cases_shortcode'); | |
/* Advocacy Filters */ | |
function multi_filter_advocacy_cases_shortcode($atts) { | |
// Get filter values from URL parameters or defaults | |
$state = isset($_GET['state']) ? sanitize_text_field($_GET['state']) : ''; | |
$issue = isset($_GET['issue']) ? sanitize_text_field($_GET['issue']) : ''; | |
// Initialize query arguments | |
$args = array( | |
'post_type' => 'advocacy', // Post type 'advocacy' | |
'posts_per_page' => -1, // Show all posts | |
'orderby' => 'title', // Sort posts alphabetically by title (optional for now) | |
'order' => 'ASC', // Sorting order | |
); | |
// Initialize tax_query for filtering by state and issue | |
$tax_query = array('relation' => 'AND'); // Ensure multiple filters work together | |
// Add State filter if specified | |
if ($state) { | |
$tax_query[] = array( | |
'taxonomy' => 'advocacy_states', // Taxonomy for states | |
'field' => 'slug', | |
'terms' => $state, | |
); | |
} | |
// Add Voting Issue filter if specified | |
if ($issue) { | |
$tax_query[] = array( | |
'taxonomy' => 'advocacy_categories', // Taxonomy for issues | |
'field' => 'slug', | |
'terms' => $issue, | |
); | |
} | |
// Apply tax_query if there are filters | |
if (count($tax_query) > 1) { | |
$args['tax_query'] = $tax_query; | |
} | |
// Query the advocacy cases | |
$query = new WP_Query($args); | |
// Start output buffer | |
ob_start(); | |
// Filter form for users to select their filters | |
?> | |
<form method="get" action="" class="litigation_form" style="display: flex; justify-content: space-between; align-items: center;"> | |
<div class="litigation_search" style="flex: 1; display: flex; justify-content: flex-start; gap: 20px;"> | |
<label for="state" style="width: 300px;"> | |
Select State: | |
<select name="state" id="state" class="litigation-select"> | |
<option value="">All States</option> | |
<?php | |
// Fetch states and order them alphabetically | |
$states = get_terms(array( | |
'taxonomy' => 'advocacy_states', // Taxonomy for states | |
'orderby' => 'name', // Alphabetical order | |
'order' => 'ASC', // Ascending order | |
'hide_empty' => false | |
)); | |
// Display each state in the dropdown | |
foreach ($states as $state_term) { | |
echo '<option value="' . $state_term->slug . '" ' . selected($state, $state_term->slug, false) . '>' . $state_term->name . '</option>'; | |
} | |
?> | |
</select> | |
</label> | |
<label for="issue" style="width: 400px;"> | |
Select Voting Issue: | |
<select name="issue" id="issue" class="litigation-select"> | |
<option value="">All Voting Issues</option> | |
<?php | |
// Fetch categories and order them alphabetically | |
$categories = get_terms(array( | |
'taxonomy' => 'advocacy_categories', // Taxonomy for issues | |
'orderby' => 'name', | |
'order' => 'ASC', | |
'hide_empty' => false | |
)); | |
foreach ($categories as $category) { | |
echo '<option value="' . $category->slug . '" ' . selected($issue, $category->slug, false) . '>' . $category->name . '</option>'; | |
} | |
?> | |
</select> | |
</label> | |
</div> | |
<div class="litigation_button" style="flex: 1; display: flex; justify-content: flex-end; align-items: center; gap: 20px;"> | |
<input type="submit" value="Filter" class="filter_litigation"> | |
<a href="<?php echo esc_url(remove_query_arg(array('state', 'issue'))); ?>" class="reset_litigations">Reset Filters</a> | |
</div> | |
</form> | |
<?php | |
// Display the filtered cases in a grid | |
if ($query->have_posts()) { | |
// Display posts ordered by state | |
echo '<div class="litigation-grid">'; // Start grid | |
$states_ordered = get_terms(array( | |
'taxonomy' => 'advocacy_states', | |
'orderby' => 'name', | |
'order' => 'ASC', | |
'hide_empty' => false | |
)); | |
// Loop through states in alphabetical order | |
foreach ($states_ordered as $state_term) { | |
//echo '<h2>' . $state_term->name . '</h2>'; // Display state name | |
// Query posts for each state | |
$state_posts = new WP_Query(array( | |
'post_type' => 'advocacy', | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'advocacy_states', | |
'field' => 'slug', | |
'terms' => $state_term->slug | |
) | |
) | |
)); | |
// Check if there are posts for this state | |
if ($state_posts->have_posts()) { | |
while ($state_posts->have_posts()) { | |
$state_posts->the_post(); | |
// Display individual post | |
echo '<div class="litigation_post">'; | |
echo '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>'; | |
echo '<p>' . wp_trim_words(get_the_excerpt(), 20, '...') . '</p>'; | |
echo '<a href="' . get_permalink() . '">Read More</a>'; | |
echo '</div>'; // End post | |
} | |
} else { | |
echo '<p>No posts available for ' . $state_term->name . '</p>'; | |
} | |
wp_reset_postdata(); // Reset post data after each state query | |
} | |
echo '</div>'; // End grid | |
} else { | |
echo 'No cases found matching the selected filters.'; | |
} | |
wp_reset_postdata(); | |
return ob_get_clean(); | |
} | |
add_shortcode('multi_filter_advocacy', 'multi_filter_advocacy_cases_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment