Skip Navigation

[Resolved] Search in Frontend | Filter only products and search in custom fields

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 3 voices.

Last updated by Mateus Getulio 2 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2779972
Bildschirmfoto 2024-10-30 um 13.03.27.png

Objective:
In our WordPress site, using Toolset to build an archive for search results, we aim to achieve the following:

1. Display Custom Post Type Only
- In the search results archive, only items of the custom post type "produkte" should be displayed (excluding posts, pages, etc.).
- Issue: In the "Query Filters" section, there is no apparent option to filter by "post_type" or similar. How can we add this filter?

2. Comprehensive Search Functionality
- Enable the standard WordPress search field to search across all fields, including custom fields associated with "produkte," without requiring a custom search form.

3. Admin Area Search
- In the WordPress admin area, searching for "produkte" should also include results from custom fields.

Site Link:
hidden link

Thank you in advance for your assistance and ideally for a direct solution!
Best regards,
Matthias

#2780004

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As per our support policy, we entertain only one question per ticket. This will help us to write problem resolution summery for the original issue reported as well as help users searching on the forum.

For now, I will focus on the 1st question and will address the other issues later on once the 1st issue is addressed.

To control the post types for the search result archive, for that, you will have to create the search result archive using the legacy view.

To enable the legacy view, you should try to follow the following Doc:
- https://toolset.com/course-lesson/enabling-legacy-version-of-toolset-views/

Once you enable the legacy views, you should try to create the Search Result archive using the legacy view and from the "Loops Selection" section select the "Search results" and as soon as you select the option "Search results" you will see "Edit" button just below that using which you will be able to control the post types you want to include.

#2780109

Hello Minesh,

Thank you for your response.

I wanted to switch the editor to the legacy version, but I received a message indicating that all content would be deleted. Unfortunately, that isn’t an option for us, as we’ve put considerable time into designing the search page with multiple conditions and customizations.

Is there a way to hook into or override the Toolset/WordPress function handling the query?

Best regards,
Matthias

#2780126

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When you say products - do you mean you installed the WooCommerce plugin and WooCommerce product post type? if yes:

Could you please send me debug information that will help us to investigate your issue.
=> https://toolset.com/faq/provide-debug-information-faster-support/

#2780127

Yes ... here is the debug info

#2780534

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

Minesh is not available today so I'll be responding on his behalf.

With the block based archive currently there is no feature what post types should be included/excluded.

In such a case you will have to use the WordPress hook: pre_get_posts

For example:
- You can add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

function func_search_archive_exclude_posts($query) {
    if (!$query->is_admin && $query->is_search) {
        $allowed_post_types = array('product');
        $query->set('post_type', $allowed_post_types );
    }
    return $query;
}
add_action('pre_get_posts', 'func_search_archive_exclude_posts');

Where:
- you can add more/adjust the post type slugs you want to allow with $allowed_post_types.

#2780584

Problem is solved. Many thanks for your help, Minesh and Mateus!