1

I have a search form with one standard text input and a select input to select a taxonomy term.

The query uses the inputs to run a WP_Query and display the results. The only issue is that the query doesn't seem to run the taxonomy parameter. I can enter a keyword in fine and it will display the correct results however it doesn't pickup the taxonomy parameter. If I don't enter any keyword but select a taxonomy term from the select input then no results are found.

The value from the select input is being passed through, it's in the URL on the search results page and I've echoed it onto the results page.

When I echo the WP_Query request it doesn't detail any attempt to lookup products in the custom taxonomy 'product-category'.

Here's the form -

<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="text" name="product-name" value="" placeholder="Enter product name or code">
    <input type="submit" value="Search">

    <select name="category">
        <option value="" disabled selected>Select category</option>

      <?php
        $post_type = 'product';
        $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type, 'order' => 'ASC', 'orderby' => 'date' ) );

        foreach( $taxonomies as $taxonomy ) :
            $terms = get_terms( $taxonomy );

            foreach( $terms as $term ) :
      ?>

        <option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
      <?php
            endforeach;
        endforeach;
      ?>

    </select>

    <input class="hidden-search" type="hidden" name="search" value="advanced">
</form>

Here's the Query -

$_keyword   = $_GET['product-name'];
$_cat       = $_GET['category'];

$product_args = array(
    's'              => $_keyword,
    'post_type'      => 'product',
    'posts_per_page' => 10,
    'taxonomy'       => 'product-category',
    'terms'          => $_cat
);

$productSearchQuery = new WP_Query( $product_args );

echo $productSearchQuery->request;

Have also tried -

$product_args = array(
    's'              => $_keyword,
    'post_type'      => 'product',
    'posts_per_page' => 10,
    'tax_query'      => array( array(
        'taxonomy' => 'product-category',
        'field'    => 'slug',
        'terms'    => $_cat,
    ) ),
);

Query Request example -

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
FROM wp_posts 
WHERE 1=1 
    AND ( 0 = 1 ) 
    AND wp_posts.post_type = 'product' 
    AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date DESC 
LIMIT 0, 10

1 Answer 1

1

The better option may be to use the pre_get_posts hook instead to modify the query before it gets requested instead of creating your own query. Here's what it would look like:

/**
 * Modify Theme Queries
 * - https://developer.wordpress.org/reference/hooks/pre_get_posts/
 * - https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
 *
 * @param $query
 *
 * @return void
 */
function theme_pgp( $query ) {

    if( is_admin() ) {
        return;                 // If we're in the admin panel - drop out
    }

    if( $query->is_main_query() && $query->is_search() ) {      // Apply to all search queries

        // IF our category is set and not empty - include it in the query
        if( isset( $_GET['category'] ) && ! empty( $_GET['category'] ) ) {
            $query->set( 'tax_query', array( array(
                'taxonomy'  => 'product-category',
                'field'     => 'slug',
                'terms'     => array( sanitize_text_field( $_GET['category'] ) ),
            ) ) );
        }
    }
}
add_action( 'pre_get_posts', 'theme_pgp' );

At this point on your search page you can just use your standard loop instead of the custom WP_Query:

if( have_posts() ) {
    while( have_posts() ) {
        the_post();

        the_title();
    }
}
5
  • I'll give this a try tomorrow as I'm clocking out now. Will I be able to set the if condition for search queries to apply to a custom search template?
    – benjosity
    Aug 2, 2016 at 16:03
  • @benjosity If you want to apply a different template - I suggest using the template_include hook.
    – Howdy_McGee
    Aug 2, 2016 at 16:06
  • Okay, this is working. Only issue is the navigation that's generated by the theme is disappearing. I'm thinking it may have something to do with the custom template however the call to the header is still the same. Everything looks normal in the header minus the navigation.
    – benjosity
    Aug 3, 2016 at 14:55
  • @benjosity - Added a new check to the conditional which will only apply to the Main Query - this will ensure other queries ( such as navigation ) will not be affected.
    – Howdy_McGee
    Aug 3, 2016 at 14:56
  • 1
    Brilliant! Worked perfectly. I'll mark this problem as solved. Thanks.
    – benjosity
    Aug 3, 2016 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.