最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - CPT Search Form with Taxonomy filter & Or

programmeradmin3浏览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.

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.

Share Improve this question edited Oct 16, 2017 at 18:11 Randomer11 asked Oct 14, 2017 at 15:21 Randomer11Randomer11 41611 silver badges31 bronze badges 2
  • Google -> learn it: webdevstudios/2015/09/01/… – ProEvilz Commented Oct 16, 2017 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 Commented Oct 16, 2017 at 18:13
Add a comment  | 

2 Answers 2

Reset to default 4 +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/wordpress/ajax-post-filters.html

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>
发布评论

评论列表(0)

  1. 暂无评论