WordPress filter by custom taxonomy and keyword search
-
I have a script which is not working properly, may I please have a second pair of eyes. I need the script to rememver which category from the select list is seleted when displaying results and I also need the keyword search to work independently or together with the taxonomy select filter.
Please have a look at my script below:
get_header(); ?> <?php /** **/ $searched = $_GET['searched']; ?> <div id="main-content" class="main-content"> <?php while ( have_posts() ) : the_post(); get_template_part( 'content', 'page' ); endwhile; ?> <?php // If the taxonomy term has previously been selected from the dropdowm menu, grab it $term = isset( $_GET[ 'foundry-category' ] ) ? sanitize_text_field( $_GET[ 'foundry-category' ] ) : false; // In this section, set selected and taxonomy_query values before creating the taxonomy dropdown & getting WP_Query results $selected = ''; $tax_query = ''; if ( $term ) { // Get all posts with the selected taxonomy term $term = get_term_by( 'slug', $term, 'foundry-category' ); if ( ! empty( $term->name ) ) { // Set the 'selected' value that we'll use to show which option in the dropdown menu is the currently selected one $selected = $term->name; // Set the taxonomy to 'tools' and the term to the selected term. We'll use these to filter the posts, below $tax_query = array( array( 'taxonomy' => 'foundry-category', 'terms' => $term, ), ); } } // search arguments if( $searched != '' ) { $args['s'] = $searched; } // pagination $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // loop arguments $args = array( 'post_type' => 'foundries', 'orderby' => 'title', 'order' => 'ASC', //'posts_per_page' => -1, 'posts_per_page' => 20, 'paged' => $paged, 's' => $keyword, 'tax_query' => $tax_query, ); // execute loop $loop = new WP_Query( $args ); ?> <div id="foundries-list" class="container"> <div class="foundry-search-filter"> <div class="block-filter block-category-filter"> </div> <div class="block-filter block-search-form"> <form class="form-search-filter" action="<?php the_permalink(); ?>" method="get"> <div class="form-item-wrapper"> <?php // Category Filter /* wp_dropdown_categories( array( 'orderby' => 'NAME', // Order the items in the dropdown menu by their name 'taxonomy' => 'foundry-category', // Only include posts with the taxonomy of 'tools' 'name' => 'foundry-category', // This field is needed for submitting the form 'value_field' => 'slug', // This field is needed for submitting the form 'show_option_all' => 'All Foundries', // Text the dropdown will display when none of the options have been selected 'selected' => $selected, // Set which option in the dropdown menu is the currently selected one ) ); */ $terms = get_terms([ 'taxonomy' => 'foundry-category', 'hide_empty' => true, ]); ?> <select name="foundry-category" id="foundry-category" class="postform"> <?php foreach($terms as $cat) { echo '<option value="'.$cat->name.'">'.$cat->name . '</option>'; }?> <option value="" selected="selected">All Categories</option> </select> </div> <div class="form-item-wrapper"> <input type="text" name="searched" id="searched" placeholder="search" value="<?php echo $searched; ?>"> </div> <div class="form-item-wrapper"> <span class="search-btn dflex-center"> <input type="submit" value="Search"> </span> </div> </form> </div> </div> <?php if( $loop->have_posts() ) : ?> <div class="foundries-list-wrraper"> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <h1><?php the_title; ?></h1> <?php endwhile; ?> </div> <?php endif; ?> </div> </div>
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘WordPress filter by custom taxonomy and keyword search’ is closed to new replies.