简体   繁体   中英

wordpress custom taxonomy search not working

Hello i am building advanced wordpress custom search with taxonomy filter. But the taxonomy is not being filtered. This is the code for the search form;

<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" role="search">
    <label for="s" class="assistive-text"><?php _e( 'Search', 'understrap' ); ?></label>
    <div class="input-group">
        <input type="text" class="field form-control" name="s" id="s" placeholder="<?php esc_attr_e( 'Search &hellip;', 'understrap' ); ?>" />
        <input type="hidden" name="post_type" value="country">

        <?php
         $args= array('hide_empty'=>false,
                      );
        $district = get_terms('country',$args); ?>
        <select name="country">
        <option value="" selected="selected"> Select</option>
        <?php foreach ($district as $region) {?>
        <<option value="albania"> <?php echo  $region->name;?></option>
            <?php } ?>  
        </select>


            <input type="submit" class="submit btn btn-primary" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'understrap' ); ?>" />
        </span>
    </div>
</form>

This the query from the url:

http://localhost/home/?s=&post_type=country&country=&submit=Search

First, make sure your taxonomy is set to public eg

register_taxonomy( 'my_tax', 
        array('for_posttype'), 
        array('hierarchical' => true,     
            'public' => true,

Then verify if you can directly query the post type + taxonomy parameter based on url query var, run directly to browser http://yoursite.com/?post_type=country&taxonomy=your_taxonomy_slug&term=term_slug

If you don't have any result then you have to do additional debugging like making sure there's already post assign into that term, the term is public etc.

When you get a result you can then build a custom form to build the url you'd want generate.

eg

<form method="GET" action="/" >
    <input type="text" name="s" placeholder="keyword" />
    <select name="term">
        <option value="" selected="selected"> Select</option>
        <option value="term1">Term 1</option> 
        <option value="term2">Term 2</option>
    </select>
    <input type="hidden" name="post_type" value="country">
    <input type="hidden" name="taxonomy" value="your_taxonomy_slug">
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM