WordPress search: Filter posts by custom taxonomy

Returning results using a custom taxonomy is easier to do it that it sounds, I will show two ways.

The link method

The first method I think is the easies because it doesn’t require to buil any form create any fields or try to learn complicated API calls. Here are a few examples, you want to add.

  • Term name:
  • /?s=search+term&taxonomy=taxonomy&term_name=term_name 
  • Term id
  • /?s=search+term&taxonomy=taxonomy&tag_ID=term_ID

Traditional method, the web form.

This method is just like the link in that the end result will look just the same, but you are giving the user a way to change the input data. Here is how your form should look like.

<form method="get" id="searchform" action="/">
<label for="s" class="assistive-text">Search</label>
<input type="text" class="field" name="s" value="" id="s" placeholder="Search">
<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search"><input type="hidden" name="taxonomy" value="taxonomy">
<input type="hidden" name="term_name" value="term_name">
</form>

 

Extra: Post Types

  • Link: /?s=search+term&post_type=post_type
  • Form: <input type="hidden" name="post_type" value="post_type">

4 Comments on “WordPress search: Filter posts by custom taxonomy

  1. Zack says:

    I was searching for something as easy like this but unfortunately this does’t seem to work. I am using wordpress version 3.3.2. Please reply. No results are returned as the parameters taxonomy & term_name is not used at all

  2. Hax says:

    I tested it on version 3.3.2 and it works just fine, taxonomy & term_name was already implemented on this version. Please provide a link to your site and taxonomy & term_name to test.

  3. zac says:

    Well nearly a month later i have returned with some different views which you would be very interested in.

    1. Get all results of a certain Custom Post Type.
    /studies/
    OR
    /?s=+&post_type=ccrrc_studies

    (The layout will vary as the template has been designed only for the archive-*custom-post-type*.php page)

    2. /?s=+&studies_categories=asia

    Here ‘studies_categories’ is the taxonomy for custom post type ‘ccrrc_studies’ and ‘asia’, ‘nacs’, etc. are the various terms for this taxonomy. The terms are created in the dashboard.

    Code :
    register_taxonomy(
    ‘studies_categories’,
    ‘ccrrc_studies’,
    array(
    ‘hierarchical’ => true,
    ‘labels’ => array(
    ‘name’ => __( ‘Study Councils’ ),
    ‘singular_name’ => __( ‘Councils’ ),
    ‘menu_name’ => __( ‘Councils’ ),
    ),
    ),
    ‘query_var’ => true,
    ‘rewrite’ => array(‘slug’ => ‘studies-categories’)
    )
    );

    I am able to get all the posts for a certain custom post type by writing its name in the URL but still no luck with the ‘&taxonomy=studies_categories&term_name=asia’ in the GET Parameters.

    Please do provide your setup for the custom post type and taxonomy so i know its an issue with setting the taxonomy & custom post type up.

  4. Hax says:

    you need to make the custom post type searchable.

    register_post_type('type', array(
    			'public' => true,
    			'show_ui' => true,
    			'revisions' => false,
    			'publicly_queryable' => true,
    			'exclude_from_search' => false,
    			'show_in_nav_menus' => true,
    				)
    		);
    
    

Comments are closed.