0

My problem is this: I have 3 custom taxonomies, let's say 'author', 'title' and 'editor', each one applied to regular posts. Let's say I have post_1 that has in the 'author' field 'jorge borges' and post_2 that has 'ray bradbury'.

I'm trying to have a search form with a drop down menu containing the three taxonomies and a text field, so that if I select, i.e., 'author' and search for 'jorge borges', the result will be post_1.

The other two taxonomies should work like this as well.

I couldn't find anything similar, as many questions concern the creation of a drop down menu with all instances of a taxonomy, which is not what I want. I want a drop down menu with taxonomy categories, not values.

So how do I do that?

3 Answers 3

0

try this: Put this code in your search form:

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/search">
<?php 
$args = array(
   'public'   => true,
   '_builtin' => false
); 
$output = 'objects';
$operator = 'and'; 
$taxonomies = get_taxonomies( $args, $output, $operator ); 
$my_tax_array = array('your_taxonomy_name1','your_taxonomy_name2',); //make array of your taxonomies
echo "<select id='my_select_taxonomy' name='my_select_taxonomy'>";
echo "<option value='0'>----</option>";
if ( $taxonomies ) {
  foreach ( $taxonomies  as $taxonomy ) {
    if (in_array($taxonomy->name, $my_tax_array)){
        echo "<option value='{$taxonomy->name}'>{$taxonomy->label}</option>";
    }
  }
}
echo "</select><br />";
?>
<input type="text" value="" name="search_field" id="search_field" />
<input type="submit" id="searchsubmit" value="Search" /> 

And in your search page this:

<?php

$search_field = $_GET['search_field'];
$my_select_taxonomy = $_GET['my_select_taxonomy'];

$args = array(
'post_type' => 'post',
'tax_query' => array(
    array(
        'taxonomy' => $my_select_taxonomy,
        'field' => 'slug',
        'terms' => $search_field,
    )
)
);
$query = new WP_Query( $args );

?>
4
  • It doesn't seem to work... Do I have to change same values? And what do you mean precisely with search page, search_form.php or search.php? Jan 30, 2014 at 17:59
  • I edited this part: $my_tax_array = array('your_taxonomy_name1','your_taxonomy_name2',); previously I left my own taxonomies names. So you should change this place inserting your taxonomies names. And search_form.php from file name i guess is where your form is and search.php i believe is where you show your search serults. So my "search page" is your search.php :) Jan 31, 2014 at 10:02
  • It's not working... The search results are still not looking inside the taxonomy terms but inside post contents. I guess I'm inserting the second part in the wrong place. Can you please paste the whole search.php page? Feb 1, 2014 at 11:39
  • ok solved! the missing part was to add the name of the query in the loop: <?php if( $query->have_posts() ) : ?> <?php while($query->have_posts() ) : $query->the_post(); ?> Feb 13, 2014 at 21:34
0

It might be able to do with this plugin (Relevanssi):

http://wordpress.org/support/topic/plugin-relevanssi-sorting-filtering-search-results

0

It's not working... The search results are still not looking inside the taxonomy terms but inside post contents. I guess I'm inserting the second part in the wrong place.

This is how my search.php looks like:

<?php

if( !defined( 'ABSPATH' ) ) {
exit;
}


$search_field = $_GET['search_field'];
$my_select_taxonomy = $_GET['my_select_taxonomy'];

$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
    'taxonomy' => $my_select_taxonomy,
    'field' => 'slug',
    'terms' => $search_field,
)
)
);
$query = new WP_Query( $args );

get_header(); 
?>
<div id="content-search" class="<?php echo implode( ' ', responsive_get_content_classes()); ?>" >

//content..

</div>

You can test for yourself here:
test page
whereas the taxonomy values for authors are 'gio ponti' or 'aavv'

I'm using a child theme of Responsive theme, may this be of any help.

5
  • @paulius any news about how to fix it? Feb 3, 2014 at 19:02
  • In search form you use this code: <input type="text" class="field" name="s" id="s" placeholder=""/> try change to this: <input type="text" value="" name="search_field" id="search_field" /> Feb 5, 2014 at 9:23
  • not working yet. any idea @PauliusJakštas ? Feb 11, 2014 at 10:59
  • I just noticed that if you try searching for very general words it works regularly: this means it is just ignoring the custom query part. In fact the results also come from pages, while in the args we put only posts. How can this be possible? Feb 11, 2014 at 13:33
  • Try to enable blank search, add this code to your functions.php file: function make_blank_search ($query){ global $wp_query; if (isset($_GET['s']) && $_GET['s']==''){ //if search parameter is blank, do not return false $wp_query->set('s',' '); $wp_query->is_search=true; } return $query; } add_action('pre_get_posts','make_blank_search'); Feb 13, 2014 at 13:13

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.