Post Grid Filter Shortcode not displaying correct results
I am using Post Grid plugin to display a catalog for a record company using custom post types and taxonomies. They want to be able to filter by artist and genre on the front end, and I have looked at the Post Grid documentation to find a shortcode php file that you can use to add this to a widget in my sidebar. I’ve gotten most everything to work, it shows up in the sidebar widget using the shortcode i generated, the 2 dropdowns are there for artist and genre, and they correctly populate in the dropdown based on the current taxonomies present. I can NOT for the life of me figure out how to make it actually filter the data after pressing ‘submit’ and it just shows everything unfiltered. Here is the code I am using.
<?php
add_shortcode('post_grid_filter_form','post_grid_filter_form');
if ( ! defined('ABSPATH')) exit; // if direct access
function post_grid_filter_form($args){
$page_url = '';
?>
<div class="post-grid-filter">
<form action="#<?php //echo $page_url; ?>" method="get">
<?php
do_action('post_grid_filter', $args);
?>
<div class="field-wrap submit">
<div class="field-input">
<?php wp_nonce_field( 'nonce_post_grid_filter' ); ?>
<input type="submit" class="" placeholder="" value="Submit">
</div>
</div>
</form>
</div>
<?php
}
//FILTER BY ARTIST
add_action('post_grid_filter', 'post_grid_filter_artist_field');
function post_grid_filter_artist_field($args){
$terms = get_terms( array(
'taxonomy' => 'artist',
'hide_empty' => false,
) );
//var_dump($terms);
$pgs_artist = isset($_GET['pgs_artist']) ? sanitize_text_field($_GET['pgs_artist']) : '';
?>
<div class="field-wrap">
<div class="field-label">Filter by Artist</div>
<div class="field-input">
<select name="pgs_artist">
<option><?php echo __('Choose Artist', ''); ?></option>
<?php
if(!empty($terms))
foreach ($terms as $term){
$term_id = isset($term->term_id) ? $term->term_id : '';
$term_name = isset($term->name) ? $term->name : '';
?>
<option <?php echo selected($term_id, $pgs_artist)?> value="<?php echo $term_id; ?>"><?php echo $term_name; ?></option>
<?php
}
?>
</select>
</div>
</div>
<?php
}
//FILTER BY GENRE
add_action('post_grid_filter', 'post_grid_filter_genre_field');
function post_grid_filter_genre_field($args){
$terms = get_terms( array(
'taxonomy' => 'genre',
'hide_empty' => false,
) );
//var_dump($terms);
$pgs_artist = isset($_GET['pgs_genre']) ? sanitize_text_field($_GET['pgs_genre']) : '';
?>
<div class="field-wrap">
<div class="field-label">Filter by Genre</div>
<div class="field-input">
<select name="pgs_genre">
<option><?php echo __('Choose Genre', ''); ?></option>
<?php
if(!empty($terms))
foreach ($terms as $term){
$term_id = isset($term->term_id) ? $term->term_id : '';
$term_name = isset($term->name) ? $term->name : '';
?>
<option <?php echo selected($term_id, $pgs_artist)?> value="<?php echo $term_id; ?>"><?php echo $term_name; ?></option>
<?php
}
?>
</select>
</div>
</div>
<?php
}
// Process form data and post query
function post_grid_query_custom_search($query_args, $args){
$current_post_id = get_the_ID();
$pgs_artist = isset($_GET['pgs_artist']) ? sanitize_text_field($_GET['pgs_artist']) : '';
$pgs_genre = isset($_GET['pgs_genre']) ? sanitize_text_field($_GET['pgs_genre']) : '';
$query_args['s'] = $pgs_artist;
return $query_args;
}
add_filter('post_grid_query_args','post_grid_query_custom_search', 90, 2);
Leave an answer