Ecommerce Shopify WordPress Discussion

Search WordPress Archive by title and/or taxonomy

I have an archive with a list of researchers, I would like to be able to filter them via form by either name and/or organization(taxonomy). Currently, I am able to search either or, but not both. For example if my dropdown menu has organizations A, B and C, and I have researchers David (A), Sally (B), Jon (B). I search David - I get the result, David. I search Organization B - I get the result, Sally and Jon. I search David and Organization B - I get a 404 page instead of no results found or David, Sally and Jon. function add_query_vars_filter($vars) { // add custom query vars that will be public // https://codex.wordpress.org/WordPress_Query_Vars $vars[] .= "organization"; $vars[] .= "r_name"; return $vars; } add_filter("query_vars", "add_query_vars_filter"); /** * Override researcher Archive Query * https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts */ function researcher_archive($query) { // only run this query if we're on the researcher archive page and not on the admin side if ( $query->is_archive("researcher") && $query->is_main_query() && !is_admin() ) { // get query vars from url. // https://codex.wordpress.org/Function_Reference/get_query_var#Examples // example.com/researcher/?rating=4 $category = get_query_var("organization", false); $name = get_query_var("r_name", false); // used to conditionally build the meta_query // the meta_query is used for searching against custom fields $meta_query_array = ["relation" => "AND"]; // final meta_query $query->set("meta_query", $meta_query_array); // used to conditionally build the tax_query // the tax_query is used for a custom taxonomy assigned to the post type // i'm using the `'relation' => 'OR'` to make the search more broad $tax_query_array = ["relation" => "OR"]; // conditionally add arrays to the tax_query based on values in the URL // `organization` is the name of my custom taxonomy $category ? array_push($tax_query_array, [ "taxonomy" => "organization", "field" => "term_id", "terms" => $category, ]) : null; // Add search parameter for post title $query->set("s", $name); // final tax_query $query->set('posts_per_page', 12); $query->set('orderby', 'wpse_last_word'); $query->set('order', 'ASC'); $query->set("tax_query", $tax_query_array); //TO DO - allow query to do name OR organization } } add_action("pre_get_posts", "researcher_archive"); archive-researcher.php <?php /** * Template Name: Researcher Archive * Description: Custom template for displaying a list of researchers. */ get_header(); ?> <div id="primary" class="content-area"> <main id="main" class="site-main"> <section> <div class="container"> <h1>Researchers</h1> <form method="GET" action="<?php echo get_post_type_archive_link("researcher"); ?>"> <!-- gather data to use in form fields --> <?php $categories = get_terms([ "taxonomy" => "organization", "hide_empty" => false, ]); ?> <div> <input type="text" name="r_name" id="r_name" placeholder="Search" value="<?php echo get_query_var("r_name", false); ?>"> </div> <div> <select name="organization" id="organization"> <option value="">Select a Organization</option> <?php foreach ($categories as $category): ?> <option id="<?php echo $category->name; ?>" value="<?php echo $category->slug; ?>" <?php echo $category->slug == get_query_var("organization", false) ? "selected" : null; ?>> <?php echo $category->name; ?> </option> <?php endforeach; ?> </select> </div> <div> <button>Search</button> <a href="<?php echo get_post_type_archive_link("researcher"); ?>">Reset</a> </div> </form> <div class="row"> <?php if (have_posts()): ?> <?php while (have_posts()): the_post(); ?> <div class="col-12 col-lg-4 d-flex"> <?php get_template_part('template/card', 'researcher'); ?> </div> <?php endwhile; ?> <?php else: ?> <div class="col-12"> <p>No researchers found.</p> </div> <?php endif; ?> </div> <?php the_posts_pagination(); ?> </div> </section> </main> </div> <?php get_footer(); ?>
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.