K
K
Kirill Gladkih2015-12-06 13:46:23
Taxonomy
Kirill Gladkih, 2015-12-06 13:46:23

How to search by category or taxonomy in Wordpress?

The essence of the problem.
There is a taxonomy "category_media" (from the WP Media Category Management plugin)
There are a couple of categories (categories) that I would like to search. Let their IDs be - 3, 5.
I want to display a search form:
- input where we fill in the text
- input[type=radio] - category 3 - input[type=radio] - category
5
- input[type=radio] - category_media taxonomy
- input submit
It's also interesting how to make your own output template for each heading when searching.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
yaroslavgrishajev, 2015-12-10
@yaroslavgrishajev

WP has a search bar template that you can override by putting a searchform.php file in your theme and putting your desired template in it. For example like this:

<form role="search" method="get" id="searchform" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
  <div>
    <label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label>
    <input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
    <input type="radio" name="cat" value="21">
    <input type="radio" name="cat" value="22">
    <input type="radio" name="cat" value="23">
    <input type="submit" id="searchsubmit" value="<?php echo esc_attr_x( 'Search', 'submit button' ); ?>" />
  </div>
</form>

This piece of code has some changes from the default template - input with the type radio (actually, as in the question :)), it will send the cat parameter with the value 21, or 22, or 23 to the search query, which in turn will display in the search results, posts that belong to the category with id = 21, 22, or 23.
As for the template, in WP, the search results template can be overridden in the same way as the search form template by placing the search.php file in the theme directory (for example ) with Template Name: Search Page , and write a query to display posts in it. And you can vary this pattern depending on the value of $_GET['cat'] .
More info is here and here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question