Skip Navigation

[Resolved] Custom Search structure

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user has ana archive template with a search form, he would like to have no initial results until the user searches with the form.

Solution:
This requires custom code. Check the example here https://toolset.com/forums/topic/custom-search-structure/#post-2180985

Relevant Documentation:
https://developer.wordpress.org/reference/hooks/pre_get_posts/

This support ticket is created 2 years, 7 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+00:00)

This topic contains 13 replies, has 3 voices.

Last updated by BillD 2 years, 6 months ago.

Assisted by: Jamal.

Author
Posts
#2174591
Wordpress archive direct.png
List view for View not found.png

I have built two Custom Searches using Views as indicated in the documentation here: https://toolset.com/course-lesson/creating-a-custom-search/
I had them working well, but in the end the Custom Search Views box and Custom Search Results Box disappeared ‘overnight’ and left me with either nothing or “view not found” (see advice in recent ticket for “view not found”).
Now I am at my third attempt and as I imagine Toolset works fine for all your clients, I must be doing something wrong.
So, please can you check that the structure I am using is correct and if not tell me what needs to change.

The original structure I used for the first two attempts was in a Page with a Container & then View and View Output - see screenshot of list view in blue starting with Container.

As you can imagine, for the 3rd attempt I thought I would try a different structure where I have started directly with the Custom Post WordPress Archive - see screenshot of list view in white starting with WordPress Archive. Here I have a direct link to the Custom Post Archive in the navigation menu, but I’m not sure this is the best way to structure the site and it seems the functions I use would need to be re-written.
Please can you advise me on the best way to build a Custom Search taking into account the issues I have had?
Thanks.
Bill

#2175453

So, please can you check that the structure I am using is correct and if not tell me what needs to change.
Hi Bill, I don't know of anything that would result in 'View not found' because of something structural in the blocks lists you've shown. From what I can see in the screenshots here, everything looks pretty good to me.

In the WordPress Archive block menu, the main structural elements you should expect to find in every custom search WordPress Archive are there, in proper order, including WordPress Archive block, WordPress Archive Search block, WordPress Archive Output block, and WordPress Archive Loop block.

In the View block menu, the main structural elements you should expect to find in every custom search View are there, in proper order, including View Block, View Search Block, View Output Block, and View Loop Block. Placing the View inside another Container block is completely acceptable, too.

Blocks is pretty good at preventing you from creating an invalid blocks structure. For example, if you try to drag a custom search filter into the results area, you'll be blocked by the system.

So if there is a recurring problem where Views disappear, I would suspect something outside of the blocks structure from being the main culprit. I checked your other ticket and saw this comment:
"When the page stopped displaying I did a full restore to a point when it should have been ok (last Friday), but it didn't change anything."
You restored the database and the file system but the problem persisted? If the database and file system were restored to a point where everything worked before, but the problem remained, that points to more of a systems issue like server-side caching, optimization issues, . One test we can try is to create a clone of the site and install it on my local environment. If the issue was server-side caching, the problem should be resolved on my local site where I have no server-side caching installed. If you'd like me to run a test like this, I'm happy to do so. I would need access to the backups you used to restore the site, or whatever backups you would like for me to use as a baseline.

Here I have a direct link to the Custom Post Archive in the navigation menu, but I’m not sure this is the best way to structure the site and it seems the functions I use would need to be re-written.
Hmm, I don't see any problem with a direct link to an archive placed in your main navigation menu, from a technical perspective. I'm not really clear how this relates to the main problem at hand, though, where Views are disappearing. Am I misunderstanding something? What functions are you referring to?

Do you have any regular site backup processes scheduled? Do you regularly backup your database? If so, is it possible we could compare two versions of the site, before and after the custom search Views disappeared?

If no such processes are scheduled, could we go ahead and create a backup now so that we have something to compare against should the problem arise again?

#2176515

Hi Christian,
Thank you for your detailed and clear reply.
Please see my comments below in bold (I hope). The bottom line is that if I don’t have the problem again, I’m all for moving forward. I think the customs search solution directly in the Archive will work for me (so without a view). At least I’d like to try it with a little assistance on specific points - see functions below). And IF there is ever another problem, I’ll come back to you to look at the before and after comparison option. I’ll stop there as there is enough info. in the paragraphs below in answer to the points you have addressed. Again, thank you very much. Best regards, Bill
***
Hi Bill, I don't know of anything that would result in 'View not found' because of something structural in the blocks lists you've shown. From what I can see in the screenshots here, everything looks pretty good to me.
In the WordPress Archive block menu, the main structural elements you should expect to find in every custom search WordPress Archive are there, in proper order, including WordPress Archive block, WordPress Archive Search block, WordPress Archive Output block, and WordPress Archive Loop block. - Great thank you for confirming this.
In the View block menu, the main structural elements you should expect to find in every custom search View are there, in proper order, including View Block, View Search Block, View Output Block, and View Loop Block. Placing the View inside another Container block is completely acceptable, too. - Great thank you for confirming these two points.
Blocks is pretty good at preventing you from creating an invalid blocks structure. For example, if you try to drag a custom search filter into the results area, you'll be blocked by the system. Yes I have noticed that thx.
So if there is a recurring problem where Views disappear, I would suspect something outside of the blocks structure from being the main culprit. I checked your other ticket and saw this comment:
"When the page stopped displaying I did a full restore to a point when it should have been ok (last Friday), but it didn't change anything."
You restored the database and the file system but the problem persisted? If the database and file system were restored to a point where everything worked before, but the problem remained, that points to more of a systems issue like server-side caching, optimization issues, .
OK Before I check with my provider, based in the US, Litespeed Cache is used and there are various options for cache settings like exclude page, Do Not Cache URLs, Query Strings, categories etc. Is there a particular question I should ask?
I still have a page with a view and the problem encountered i.e. “file not found” message. Behind it all the code is there if you would like to check it out. However, I’m just as happy to find a solution and I am leaning towards the “WordPress Archive Search block” with filters (and no view).

One test we can try is to create a clone of the site and install it on my local environment. If the issue was server-side caching, the problem should be resolved on my local site where I have no server-side caching installed. If you'd like me to run a test like this, I'm happy to do so. I would need access to the backups you used to restore the site, or whatever backups you would like for me to use as a baseline.
Here I have a direct link to the Custom Post Archive in the navigation menu, but I’m not sure this is the best way to structure the site and it seems the functions I use would need to be re-written.
Hmm, I don't see any problem with a direct link to an archive placed in your main navigation menu, from a technical perspective. I'm not really clear how this relates to the main problem at hand, though, where Views are disappearing. - I agree it doesn’t (I don’t think) - it’s an alternative structure that I am using to avoid the ‘view not found issue’.
Am I misunderstanding something? What functions are you referring to?
So, in the second structure I use(d) a view and the functions relate to the view.
In the first structure and the one I think I would like to go with, there isn’t a view, so the following functions will presumably not work as they require and act on views. However, I am not sure about this and would appreciate your advice. How can I modify or include these functions to act on the search fields directly in the Custom Post Archive (without a view):
Here is a list of the functions that I managed to achieve with help from support - some are in in custom code in settings:
• Start with a blank page displaying no posts until the Search Filter is submitted/launched
• If the Search Filter is empty and then it is submitted/launched, do nothing, remain blank i.e. do not search the total number of posts
• Display only the number of custom posts found when the Search Filter is submitted using [wpv-found-count] - DO NOT display the total number of posts at all (including not before the search filter is submitted).

Do you have any regular site backup processes scheduled? Do you regularly backup your database? If so, is it possible we could compare two versions of the site, before and after the custom search Views disappeared? Yes I have 30 days of backups with Jetpack.
If no such processes are scheduled, could we go ahead and create a backup now so that we have something to compare against should the problem arise again? Should be ok

#2176523

deleted

#2176525

repeat deleted

#2176527

repeat deleted

#2176529

repeat deleted

#2177875
Screen Shot 2021-09-23 at 3.41.18 PM.png

OK Before I check with my provider, based in the US, Litespeed Cache is used and there are various options for cache settings like exclude page, Do Not Cache URLs, Query Strings, categories etc. Is there a particular question I should ask?
I don't have any specific questions to ask yet, and I think it would be prudent to confirm the problem is cache-related first. If the problem occurs again I would try disabling LiteSpeed cache completely to see if that solves the problem. If so, then re-enable Litespeed and try each of the individual cache purging options to see which one solves the problem. Then we can ask more informed questions.

I still have a page with a view and the problem encountered i.e. “file not found” message. Behind it all the code is there if you would like to check it out. However, I’m just as happy to find a solution and I am leaning towards the “WordPress Archive Search block” with filters (and no view).
How can I modify or include these functions to act on the search fields directly in the Custom Post Archive (without a view):
I'm not able to access wp-admin, rechercher-2, or even the homepage of the site because of a security feature - maybe WordFence? Screenshot attached here showing what I see. What do you recommend?

#2179657

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello there! Christian won't be available. If you don't mind I'll continue with you on this ticket.

I also could not log in, most probably, because of Wordfence. Check this screenshot hidden link
If the restriction is by country, I am located in Morocco.

As soon as I log into your website I'll check it and get back to you with my findings.

#2180145

Hi Jamal,
Thanks for offering to take over the ticket.
I'll take you up on your offer of support on the next ticket.
I'll stick with Christian on this one as as this is my 3rd attempt to resolve this issue and find a solution which means I have provided information 3 times already and credentials too, so I would prefer to see this one through with Christian as I feel we are nearly there.
Thank you again.
Bill

#2180161

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Bill,

Unfortunately, Christian won't be able to work on support any time soon. If you would prefer to get someone on the Americas timezone, I can transfer this to Shane https://toolset.com/forums/users/shane-c/

To answer your question about the features from the custom codes, the same features can be implemented for the archive template, but the code will need to be adapted. Because views offer their own filters that we can hook into, but archive templates rely on the default WordPress hooks, especially "pre_get_posts".

Regarding the "view not found" issue, I am afraid, I can't say anything without doing at least some debugging. What would be helpful, is if we figure out the steps that produce that error. Random errors are very hard to debug. Having clear steps to reproduce an issue is half the solution. Even, If I can fix it, I can escalate it to a developer who, for sure, will fix it.

If you need assistance with the custom code for the archive template, I'll need to check the archive and see what are the fields involved in the search area. Let me know your feedback.

#2180231

Ok Jamal. Thanks for telling me and for getting up to speed on the problem so quickly.
You should be able to access the site now - changed Wordfence. European time suits me.
Good know about the WordPress hooks and that it these features can be reproduced on the archive.
My priority is to move this along and try and get the setup I'm looking for. So any assistance you can provide would be very welcome. Most of the information should be in the previous exchanges, but let me know if you need anything from me.
Thanks again.
Bill

#2180985

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Bill,
Toolset does not offer a proper filter to change the archive query in the same way it does for views(wpv_filter_query). So, we need to rely on the default WordPress filter "pre_get_posts".
https://developer.wordpress.org/reference/hooks/pre_get_posts/

However, this filter is run for every WP_Query. So, we must ensure that we change only the query of the archive template. I come up with the following code, which I added to the snippet "archive-empty-until-search":

add_filter( 'pre_get_posts', 'my_archive_until_search', 100000000, 1 );
function my_archive_until_search( $query ){
	if ( ( $query->get('post_type') == 'catena' ) && $query->is_post_type_archive() && !is_admin() ) {
		if ( (!isset($_GET['wpv_post_search'])) || ( strlen($_GET['wpv_post_search']) == 0 )) {
			$query->query_vars['post__in'] = array(0);
			$query->set('post__in', array(0));
		}
	}
}

In line 3, we check if this query is for the archive that we want to affect.

The number of results relies on this archive's query, so it will return the number of found results, not the count of all posts.

I hope this helps. Let me know if you have any questions.

#2181107

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.