0

(Fairly new to WordPress, but I'm a React / NodeJS developer)

I have a basic search function that search a custom post_type (listings).

It makes use of the standard built in get_search_query and therefore it can only find the title of a listing - we want the search to search through the taxonomy as well.

The search URL looks as follows, http://localhost:8888/?s=New+York&post_type=listings - basic.

The post_type=listings is defined as a hidden input in the form, eg <input type="hidden" name="post_type" value="listings" />

We have custom taxonomy in each listing eg, location and tag.

After a lot of googling, I think you cannot search custom taxonomy by simply adding extra tags - at least Taxonomy is not that magical and I'd have to write a script in my functions.php.

Any suggestions on where to get started writing a custom query?

1 Answer 1

0

First you want to make the query keys available so you can check them later:

add_action( 'init', 'custom_add_query_vars' );
function custom_add_query_vars() {
    add_query_var( 'location' );
    add_query_var( 'tag' );
}

Then you want to set a tax_query by filtering the WP Query:

add_filter( 'pre_get_posts', 'custom_search_tax_query' );
function custom_search_tax_query() {

    // check this is a search query with post type of 'listings'
    if ( is_search() && get_query_var( 'post_type') 
    && ( get_query_var( 'post_type' ) == 'listings' ) ) {

        // check for location query var
        if ( get_query_var( 'location' ) ) {
            $location_query = array(
               'taxonomy' => 'location',
               'field'    => 'slug',
               'terms'    => get_query_var( 'location' ),
            );
        }

        // check for tag query var
        if ( get_query_var( 'tag' ) ) {
            $tag_query = array(
               'taxonomy' => 'tag',
               'field'    => 'slug',
               'terms'    => get_query_var( 'tag' ),
            );
        }

        // create the tax_query array
        if ( isset( $location_query ) && isset( $tag_query ) ) {
            $tax_query = array( 'relation' => 'AND', $location_query, $tag_query );
        } elseif ( isset( $location_query ) ) {$tax_query = array( $location_query );}
        elseif ( isset( $tag_query ) ) {$tax_query = array( $tag_query );}

        // finally set the tax_query
        if ( isset( $tax_query ) ) {$query->set( 'tax_query' => $tax_query );}
    }
}

Then you should be fine to pass the inputs from your search form to the keys location and tag and get the responding posts you desire. :-)

Notes:

  • There is no need to return $query from the filter as it is passed by reference.
  • Strange as it may seem, the double-nested array of $tax_query is actually correct to allow for the relation argument - whether relation is set or not.
  • Be careful with tag, if you are actually using the standard tag taxonomy it will need to be changed to post_tag (the taxonomy name for standard tags) in $tag_query, but here I assume you have added an extra taxonomy actually called tag.

Your Answer

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

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