0

I currently use the form below :

<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">

        <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Type Business or Town', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />

        <input type="text" value="<?php echo get_query_var('location'); ?>" name="location" id="location" class="headersearch location" placeholder="<?php esc_attr_e( 'Enter your county..', 'search-cust' ); ?>" />

        <input type="hidden" name="post_type" value="business" />
        <input type="submit" class="button radius tiny success" style="width: 100%;" value="<?php esc_attr_e( 'Search...', 'search-cust' ); ?>" />
</form>

I also have this in functions.php :

    function custom_cpt_search( $query ) {
       if ( is_search() && $query->is_main_query() && $query->get( 's' ) ){
            $query->set('post_type', array('business'));
        }
        return $query;
    };
add_filter('pre_get_posts', 'custom_cpt_search');

Ideally id like to have the second input as a drop-down taxonomy list instead of a type-in input, Ideally listing ALL categories in the taxonomy instead of only categories with posts in them. With the first dropdown item being a placeholder such as "Select your county".

Edited to make clearer, and remove part of the question I have resolved.

  • Google -> learn it: webdevstudios.com/2015/09/01/… – ProEvilz Oct 16 '17 at 15:51
  • Thanks for that reference, I have solved the problem with the search not finding all the data, turns out Its because alot of the page entries ive used custom fields and the default search doesn't search those without extending the function. Original Question still remains though, unsure of how to change the second field into a populated taxonomy list instead of type in. – Randomer11 Oct 16 '17 at 18:13
2
+50

I think you can do it in two following steps:

  1. In HTML code just add a dropdown with the values you need.

    <select name="yourselect"><option value="1">Value 1</option>
    
    ...
    
    </select>
    
  2. In your pre_get_posts filter add tax_query parameter like this

    if( !empty( $_GET['yourselect'] ) ) {
        $query->set('tax_query', array(
            array(
                'taxonomy' => 'YOUR_TAXONOMY_NAME',
                'field' => 'id', // or slug if you want
                'terms' => $_POST['yourselect']
            )
        );
    }
    

I'm not sure if it is a good idea to configure this way the default WordPress search. A couple months ago I already implemented similar functionality using AJAX filters. Example is here, hope it helps https://rudrastyh.com/wordpress/ajax-post-filters.html

1

After Misha answered above, a lightbulb came on which lead me to understand something a little more, this is the form which is now working as expected using wp_dropdown_categories and some args.

I had previously done it this way, but it seems the main problem previously was that it was searching for the ID in the URL instead of the slug, for some reason the id doesnt find the posts, but using the slug did ( if someone can tell me why that would be great for my learning )

This is the working form, to note ( location ) is my custom taxonomy so anyone using this will need to change it to match their taxonomy.

<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">

        <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search Business or Service', 'placeholder' ) ?>" value="<?php echo get_query_var('business'); ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />

            <?php $location_args = array('taxonomy' => 'location', 'value_field' => 'slug', 'name' => 'location', 'show_option_none' => __( 'Select County' ),'option_none_value' => '0', 'order' => 'ASC', 'hide_empty' => 0); ?> 
            <?php wp_dropdown_categories($location_args); ?>

   <input type="hidden" name="post_type" value="business" />
    <input type="submit" class="button" style="width: 100%;" value="<?php esc_attr_e( 'Search...', 'custom' ); ?>" />

</form>
  • Not working it at my side. help needed – Super Model Oct 10 '18 at 9:14
  • I need help, your script is not working at my side. – Super Model Oct 10 '18 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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