Skip Navigation

[Resolved] View Search – CPT and selections

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.

This topic contains 0 replies, has 1 voice.

Last updated by Christopher Amirian 4 weeks, 1 day ago.

Assisted by: Christopher Amirian.

Author
Posts
#2788671

Sorry, Subject should read Custom Field not CPT

I created a new search view using blocks.
I then set up a query filter choosing specific categories.
I then setup the search section adding 2 searches for 2 Custom fields and category.

Problems...
(1) When I setup the search section my category filter was changed to select ALL categories.
(2) When I got to the front end the Custom field filter for explore-towns (Town) does not list the towns.

What is the link to your site? hidden link

#2788699

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. I need to check how you setup the view as I see that the TOWN is a normal text input and not a select box.

I'd appreciate it if you could give me the URL/User/Pass of your WordPress dashboard after you make sure that you have a backup of your website.
It is absolutely important that you give us a guarantee that you have a backup so if something happens you will have a point of restore.

Make sure you set the next reply as private.

Also tell me which view to check where you created the search.

Thanks.

#2788941

Would be great if I could get a response

#2788973

Christopher Amirian
Supporter

Languages: English (English )

Screenshot 2024-12-19 at 5.34.57 PM.png

Hi,

Thank you for the login information and sorry for our late reply. Just FYI, the next two days are my off days so you may get an answer to future questions 2 days later.

I checked the Municipal and the issue is in the way you added the field.

If you go to Toolset > Custom Fields > Fields Group for Posts and check the Explore Municipals you will see that you added an option Choose with an empty value and that is the problem cause.

You need to change that to All Municipals and it should work, or avoid adding an empty value there.

For the Explore and sub categories you can not select the view that way while you have a front end.

If you check the test view I added in the draft mode here:

hidden link

If you select the view and check the content settings, there is a section called Qurey Filter that you can use to select the category in question.

But as mentioned that is not possible if you use the same category as a front end filter select box.

Thanks.

#2789027

Yes, as I said the query filter broke when I setup the search section. However, I am not sure I understand your "is not possible if you use the same category as a front end filter select box."

So, is there no way that I can restrict the page to specific parent and child categories?

#2789564

Christopher Amirian
Supporter

Languages: English (English )

Screenshot 2024-12-22 at 5.22.11 PM.png

Hi,

Yes, you can if you remove the front-end search section for the category. If you check it does not have any meaning to have a search section for the categories because you will already filter the result by the category in the first place.

So remove the category from the search section and then you will be able to use the Query Filter functionality to filter by category for the default result. (Please check the screenshot)

Thanks.

#2789613

The whole point and purpose of the page is to be able to filter by Municipal, Town and/or sub-category of the posts within the 'Explore' parent category and its sub-categories, so, yes having categories is necessary and important.

I have gotten around this by creating another custom category 'Explore' with its sub-categories.

#2789657

OK, so when I wrote my last post I though I'd got a solution, but in fact I still haven't. This should be so easy to do - is it really not possible?

You say "remove the front-end search section for the category. If you check it does not have any meaning to have a search section for the categories because you will already filter the result by the category in the first place." Well, yes I NEED a filter to search by the sub-categories.

You don't seem to understand what I want. I'm trying to explain and I would've thought it pretty clear, but here I try again.

The page will show ONLY the posts within the sub-categories that have the parent 'explore'. It will allow people to filter either by municipal, town, or category.
So they could, say, choose Antigua (municipal) / Caleta (town) / Museums (Sub-category of explore). Or they could indeed simply choose Museums.

The page is about exploring the island, I don't want the default output to display irrelevant posts from other categories

Is this really impossible to do? Is there really no way it can be done by any method?

#2789704

Christopher Amirian
Supporter

Languages: English (English )

Hi,

It seems that you did not understand me correctly. I already mentioned that youc an not have both. So you can not have pre-filtering and have front end filtering on the same category.

The way that you can go forward is to use the pre-filtering with the Query filter option of the view.

That will show only the category you want as the default.

Then you need to add the front end filter via custom code. My colleague gave a solution here that you can check:

https://toolset.com/forums/topic/filtering-a-view-by-category-on-frontend-when-the-query-filter-is-also-category/#post-2140943

Allow filtering within these pre-filtered results by adding dropdown filters for Municipal, Town, and sub-categories.

The dropdown for sub-categories should exclude the pre-filtered 'Explore' parent category and its sub-categories, as they are already used in the initial query filter.

A custom filter can be added using PHP to dynamically adjust the query on the back-end. Here's an outline of the approach:

add_filter('wpv_filter_query', 'filter_view_by_category', 10, 3);
function filter_view_by_category($query_args, $settings, $view_id) {
    if ($view_id == YOUR_VIEW_ID) {
        $query_args['tax_query'] = [
            [
                'taxonomy' => 'YOUR_TAXONOMY',
                'field' => 'slug',
                'terms' => ['sub-category-slug-1', 'sub-category-slug-2'], // Replace with relevant sub-category slugs
                'operator' => 'IN'
            ]
        ];
    }
    return $query_args;
}

This will limit the results to posts in the desired sub-categories before applying additional front-end filters.

You may need to disable AJAX if the default behavior doesn't align with your needs.

For the code to work, you need to replace the following placeholders with the correct information from your setup:

1. YOUR_VIEW_ID

The unique ID of the View where the query filter will be applied.
For you it is: 3642

2. YOUR_TAXONOMY

The taxonomy slug for the categories or terms you are filtering.

Example: If your taxonomy is category, replace YOUR_TAXONOMY with category.

3. sub-category-slug-1, sub-category-slug-2

The slugs of the sub-categories you want to pre-filter.

Example: If the sub-categories are museums and parks, replace this with 'museums', 'parks'.

4. slug-1, slug-2 in restrict_terms_in_dropdown

The slugs of the categories or terms to exclude from the front-end dropdown filter.

Example: If you want to exclude explore and featured, replace this with 'explore', 'featured'.

5. YOUR_TAXONOMY in restrict_terms_in_dropdown

The taxonomy slug for the dropdown filter's terms.
Example: If the dropdown filter is for the taxonomy category, replace this with category.

Please note: The reply time will be longer than normal due to holidays.

#2789805

Yes, I did understand you which is why I created another category type. However, I was a bit off with that when I first tried it

I have now created a taxonomy type called 'filter'. With this I simply select the taxonomy of the filter. This is a much easier way of doing it as it will allow me to produce similar pages using different 'filters' without the necessity of writing code for each one.

I thought you might be interested in this method as an alternative.

#2789822

Christopher Amirian
Supporter

Languages: English (English )

Thank you for sharing your solution. Let's keep it here so your solution can be searchable if other customers search for it.

Thanks.