Skip Navigation

[Resolved] search box for search limited to 3 taxonomies

This support ticket is created 7 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 7 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#552286

Tell us what you are trying to do?
Create a Search box - preferably with autocomplete. The search needs to be limited to searching 3 taxonomies

Is there any documentation that you are following?
hidden link

Is there a similar example that we can see?

What is the link to your site?
Is this something I can create with Views or should I use a plugin like "Search & filter" (which I tried but didn't have any success limiting the search). Any code snippets or examples much appreciated.
Thanks for your help
David

#552553

Hi,
When you say you want to limit the search to 3 taxonomies, I assume you mean that you want to predefine 3 taxonomy terms that will be used as a filter for a search. Is that right? Or did you mean you want the user to select 3 terms manually? I'm assuming you meant the first one - predefined terms. If not, let me know. I can help you set up a filter that only searches 3 predefined taxonomy terms, but there is not an option to display a text field with autocomplete in the Search Filter Controls.
- Create a new View using full custom display mode
- Select the post type you would like to search in the Content Selection area
- Add a filter in the Query Filter section. Filter by the taxonomy that includes the 3 terms
- Select the 3 predefined terms in the Query Filter settings
- Add another filter in the Query filter section. Filter by post search, and choose "Search for a text, set by the View shortcode attribute".
- Scroll down to the Filter Editor section. If you cannot see it, use the Screen Options tab in the top right corner of the screen to enable it. Add your Text Search filter control here, optionally followed by a submit button control.
- Build your Loop Output to display the search results as you would like
- Insert your View somewhere to display it on your site

#552649
taxonomy-example.JPG

Hi Christian,
Thanks for your reply, I'm not to clear on what the terms mean so I've tried to explain in the attached screenshot.

Thanks
David

#552778
Screen Shot 2017-07-25 at 5.35.45 PM.png
Screen Shot 2017-07-25 at 5.22.36 PM.png

Okay thanks, I have attached a picture showing the difference between "taxonomy" and "term". Terms are part of a taxonomy. WordPress text search is not designed to search taxonomy terms associated with posts, only post title and content. The text search filter provided by Views implements the standard WordPress text search, so it will not find results that include matching taxonomy terms. This is a WordPress limitation, not a Toolset limitation. Modifications to the WordPress text search will require custom code.

If you use the Relevanssi plugin, it's possible to search not only post title and content, but also custom fields. We have more information about this here:
https://toolset.com/documentation/user-guides/searching-texts-custom-fields-views-relevanssi/
Note that Relevanssi does not help you search by taxonomy terms. In order for this to work, you would be required to change from using taxonomies for arabic, hebrew, and other. Instead you would have custom fields for arabic, hebrew, and other. You could make these custom fields repeatable, so multiple names could be added in each language. More information about custom fields here:
https://toolset.com/documentation/user-guides/using-custom-fields/
https://toolset.com/documentation/user-guides/repeating-fields/

Views does provide taxonomy filters, but none of them are text inputs or autocomplete. I'm attaching another screenshot that shows the options that are available. Note that in your View's filter controls, each taxonomy must be displayed separately - you cannot combine terms from multiple taxonomies in a single filter. Modifications to this would require custom code.

I hope this helps you understand the options that are available in Views to create filters for your search results. Please let me know your thoughts on these ideas. It's unfortunate that we do not offer exactly what you're looking for, but if you have any questions about the methods I've described, please let me know.

#552799

Hi Christian,
I already have a plugin I copy/pasted together which I use together with the "Chosen for WordPress" plugin to produce autocomplete select lists for individual taxonomies. Here's the code:

<?php
/**
* Plugin Name: Select plant by taxonomy tag term
* Description: Create select dropdown list of taxonomy terms, select term to open plant details page. 
*              Select box uses jQuery Chosen plugin. 
* Version: 1.0 
* Author: David Myers
*/

function select_post_by_tax_tag($atts) {
    extract(shortcode_atts(array(
          'list_name' => 1, ), $atts));
    $args = array(
        'orderby'       => 'name',
        'order'         => 'ASC',
        'hide_empty'    => 1,
        'taxonomy'      => $list_name, //change this to any taxonomy
    );
        $select_box = '';
    foreach (get_categories($args) as $tax) :
        $args = array(
            'post_type'         => 'plant', //change to your post_type
            'posts_per_page'    => 1,
            'orderby'           => 'title',
            'orderby'           => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy'  => $list_name, //change this to any taxonomy
                    'field'     => 'slug',
                    'terms'     => $tax->slug
                )
            )
        );
        if (get_posts($args)) :

        foreach(get_posts($args) as $p) : 
		if (strlen($tax->name) > 2) 
          $select_box.='<option value="' . get_permalink($p) . '">' . $tax->name . '</option><br/>';
        endforeach;
        endif;
    endforeach; 
	return $select_box;
}
 
add_shortcode('post_by_eng_name', 'select_post_by_tax_tag');


?>

My coding skills are pretty weak, so I'm not sure if this could be a base for combining selected taxonomies?
Alternatively I was considering creating a hierarchical taxonomy with each of the required taxonomies being a parent of the child terms I need to search?
Does any of this sound feasible?

Thanks for your help
David

#553002

My coding skills are pretty weak, so I'm not sure if this could be a base for combining selected taxonomies?
It might be part of a complex solution. Types filters each taxonomy separately using different URL parameters - something like hidden link. You would have to write custom code that converts your custom filter selections into data Types can understand...and this isn't something we can help you with in the forum. Another challenge will be interpreting the URL params on page load and selecting the appropriate options in your custom filter. There are several other things to consider but these are the first examples that come to mind. It's difficult to say what the problems will be, since this is an unsupported feature.

Alternatively I was considering creating a hierarchical taxonomy with each of the required taxonomies being a parent of the child terms I need to search?
Sure, this is possible. You still don't have the ability to use a Text Search to filter posts by hierarchical taxonomy term. You could use a single select field, or a set of checkboxes. However, I think this would get out of hand very quickly since you could have many different terms in each taxonomy.