0

I have a number of custom post types which share a custom taxonomy.

I'm trying to implement a dropdown search box using php (to insert into a widget or elsewhere) that allows selection of a taxonomy term, and returns the posts which have that term, but only within a specific post type.

I'm hoping to then create one of these dropdown search boxes for each post type so a user can easily navigate to posts under a taxonomy term and within a post type.

Can anyone assist with this please?

1 Answer 1

1

First create a function to display your search form

function display_custom_search_form($atts){
    $return = '<form role="search" method="get" id="searchform" action="'.home_url( '/' ).'">
                    <div><label class="screen-reader-text" for="s">Search for:</label>
                    '.wp_dropdown_categories(array('name' =>'taxTerm', 'taxonomy' => 'TAXONOMY_NAME').'

    <!-- this is the magic field you will need to change in order for this to work with each post types--->

                        <input type="hidden" name="post_type" value="post" />

                        <input type="submit" id="searchsubmit" value="Search" />
                        <input type="hidden" name="custom_search" value="1"> 
                    </div>
                </form>';
    if ($echo){
        echo $return;
    }else{
        return $return;
    }
}

hook this function as a shortcode so you could use it anywhere you want (post/page/widget)

add_shortcode('my_c_s','display_custom_search_form');
add_filter('widget_text', 'do_shortcode');

then you check if the form is submitted and if so you can filter the posts based on that:

//was the form submitted

if (isset($_GET['custom_search']) && $_GET['custom_search'] == 1){
    add_filter('pre_get_posts','Custom_Search_Filter');
}

 //function to actually filter the posts based on the taxonomy term and post type
function Custom_Search_Filter($query) {
    //only do this on search
    if (is_search()){
        //first check for the first type if not set then its the default post.
        $post_type = $_GET['post_type'];
        if (!$post_type) {
            $post_type = 'post';
        }
        $query->set('post_type', $post_type);

        //then check for the taxonomy term
        if (isset($_GET['taxTerm'])){
            $query->set('TAXONOMY_NAME', $_GET['taxTerm']);
        }
    }

    return $query;
}  

copy all of the code in to you theme's functions.php and change TAXONOMY_NAME to your actual custom taxonomy name (its there twice).

open your widgets panel and add a simple text widget with [my_c_s] inside and save.

1
  • Thanks for this. I managed to find a widget that does this for me though. For anyone else interested, the Pro version of GD Custom Posts And Taxonomies Tools has a couple of widgets that allow selection of post type and taxonomy for a drop down selection box. Seems to work well for now, although the aobe would allow insertion of code anywhere so may be more suitable for some.
    – Rob
    Jul 26, 2011 at 8:52

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.