Home › Forums › Trusted Pro › Search pop-up misbehavior… pull down does not work.
Tagged: custom post type, popup, search
- This topic has 9 replies, 2 voices, and was last updated 6 years, 8 months ago by Adri Oosterwijk.
-
AuthorPosts
-
February 6, 2018 at 9:56 am #3696Adri OosterwijkParticipant
Hi,
I recently noticed that the search popup is not behaving the way it should.
When hitting the pull down menu nothing is happening. Performing a search it seems that it is only searching the products custom type. I can’t select any other type or custom type.
I have created a custom post type using WP-Types, a part of Toolset. Disabling the Toolset plugins (and therefor the custom post type?) is not solving it.
I tried to download the YITH Woocommerce Ajax Search plugin but that did not help either.
What should I do?
Best wishes,
Adri
February 6, 2018 at 11:15 am #3697AndyKeymasterThe way the search works is if WooCommerce is not active it uses the standard WP get_search_form() function.
If WooCommerce is active then it makes use of the theme’s custom search function – trusted_woocommerce_search_form() – to search WooCommerce products.
The trusted_woocommerce_search_form() function can be overridden in a child theme. Look in extras.php line 817 to 841, copy the function to the child theme’s functions.php file and remove the part that displays the WooCommerce categories dropdown (line 823 to 833).
Now the important part to change is line 835:
<input type="hidden" name="post_type" value="product">
where you should change ‘product’ to the name of your custom post type.February 6, 2018 at 12:19 pm #3701Adri OosterwijkParticipantMmmm, I possibly misunderstand you. I did understand the dropdown had the purpose to let me select
1 -> Products (Dutch: Producten)
2-> Another custom post type (in my case Recepies -> Dutch: Recepten).
3 -> Posts and Pages (Dutch: Berichten en Pagina’s)Browsing through the code I learned (like you said) that it will only list Product categories.
The reason that I want it this way is that this will be a shop with a very limited amount of products. All uncategorized.
The amount of Recepies however can be much more and will grow over time. Next to that would it be great in may case that the visitor can search the rest of the site as well.When I get this right it can keep my sidebars much cleaner. As it is now I have to add several search widgets and that makes the layout of the pages a bit messy for the visitors. The search option of the theme is great for this…..
Any chance that it can be changed in an easy wat to get what I want?
Best wishes,
Adri
February 6, 2018 at 2:35 pm #3702AndyKeymasterThe dropdown allows you or your site visitors to search within a WooCommerce product category. It does not work for selecting within post types as you listed in 1, 2, 3 above.
OK, a better way for you achieve what you want and let you filter the search to search within specific post types.
Add this code to the child theme’s functions.php file (this overrides our WooCommerce search form with the standard WP search form)function trusted_woocommerce_search_form() { get_search_form(); }
Now let’s assume you have a custom post type named recipe, add this code to the child theme’s functions.php file
function trusted_custom_search_filter( $query ) { if ( $query->is_search ) { $query->set( 'post_type', array( 'recipe' ) ); } return $query; } add_filter( 'pre_get_posts', 'trusted_custom_search_filter' );
this code filters the standard search to search within posts that are recipes (your custom post type).
If you wanted to expand the search to also search within other post types, you would simply include more post types in the post_type array.
For example, to search recipes, posts and products, the code would be like so:function trusted_custom_search_filter( $query ) { if ( $query->is_search ) { $query->set( 'post_type', array( 'recipe', 'post', 'product' ) ); } return $query; } add_filter( 'pre_get_posts', 'trusted_custom_search_filter' );
February 6, 2018 at 3:31 pm #3703Adri OosterwijkParticipantHi Andy,
Thank you for your quick reply and outstanding help. I did what you suggested and now I can search in any post (recipes, products and other (default) posts and pages. I do have another wish however…. sorry :-). Is it possible to keep the pull down menu to select what to search?
1 -> All
2-> Products
3-> Recipes
4-> Posts and PagesI hope you can provide me with a simple solution…..
Thanks in dvance.
Best wishes,
Adri
February 6, 2018 at 4:55 pm #3704AndyKeymasterSorry no simple solution.
Just thinking out loud here, do any of the plugins you looked at let you select your 4 options?
If so, and if the plugin has a widget (or shortcode) you could place it in the theme’s Top Bar widget area and use that instead.
February 7, 2018 at 6:43 am #3708Adri OosterwijkParticipantHi Andy,
The plugins I’ve looked at did not have this functionallity (as far as I know).
For now, I’m happy the way it is.
I will look into an option to precede the titles on the results page with: Product, Recipe or Post.
I think it can be achieved when I customize the search result page with a Case statement:Case post type = product echo “Product” and the Title
Case post type = recipe echo “Recipe” and the Titleand so on.
As I’m not a coder this will be a challenge …….
Best wishes,
Adri
March 1, 2018 at 2:28 pm #3988Adri OosterwijkParticipantHi Andy,
I have almost accomplished what I what I need. In the image below you can see that the titles are labelled with the post type it belongs to. (“In de winkel” for Products, “In recepten” for recipes and so on)
The issue I’m facing and I’m looking for a couple of days now is that sometimes a product has to be omitted from the search results. This is when the option “Hidden” is checked for Catalog visibility. See image below.
The function you provided me (see above) works great, except for this little thingy.
Would you be so kind to help me out with it?
Thank you very much in advance.
Best regards,
Adri
March 1, 2018 at 4:06 pm #3990AndyKeymasterYou just need to add this to exclude products where the ‘product_visibility’ taxonomy is set to ‘exclude-from-search’:
$query->set( 'tax_query', array( array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'exclude-from-search', 'operator' => 'NOT IN', ) ) );
So the full code using the previous example would be:
function trusted_custom_search_filter( $query ) { if ( $query->is_search ) { $query->set( 'post_type', array( 'recipe', 'post', 'product' ) ); $query->set( 'tax_query', array( array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'exclude-from-search', 'operator' => 'NOT IN', ) ) ); } return $query; } add_filter( 'pre_get_posts', 'trusted_custom_search_filter' );
March 1, 2018 at 4:13 pm #3991Adri OosterwijkParticipantThis works like a charm. Thank you very much. You rock! I will mark it solved now.
Best regards,
Adri
-
AuthorPosts
- The topic ‘Search pop-up misbehavior… pull down does not work.’ is closed to new replies.