Skip Navigation

[Résolu] How to stop Views looping out ALL the results even which filter value is blank?

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

After submit the search form, what I would want to see is NO results and just the message "You must enter a shipment code."

Solution:

You can try these:

https://toolset.com/forums/topic/how-to-stop-views-looping-out-all-the-results-even-which-filter-value-is-blank/#post-1880135

Relevant Documentation:

This support ticket is created Il y a 3 années et 11 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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: Asia/Hong_Kong (GMT+08:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par PaulS4783 Il y a 3 années et 11 mois.

Assisté par: Luo Yang.

Auteur
Publications
#1879937

lien caché

If you check this page, you can see that even if there is nothing entered into the "Enter Shipment Code" field, it still lists out the details of every post of that post type.

What I would want to see is NO results and just the message "You must enter a shipment code."

This was working previously but at some point a plugin update may have broken things.

This is a clone of the site for you to investigate. It's probably just a simple change to the Views query.

lien caché

Search and Pagination

[wpv-filter-start hide="false"]
[wpv-conditional if="( '[wpv-search-term param='wpv_post_search']' eq '' ) AND ( '[wpv-search-term param='wpv_filter_submit']' ne '' )"]
<strong class="input-missing">[wpml-string context="wpv-views"]You must enter a shipment code.[/wpml-string]</strong><br><br>
[/wpv-conditional]
[wpv-filter-controls]
<div class="form-group"><div class="search-shipments-box">[wpv-filter-search-box placeholder="[wpml-string context="wpv-views"]Enter Shipment Code[/wpml-string]" output="bootstrap"]</div></div>
[wpv-filter-submit class="my-button" name="[wpml-string context="wpv-views"]Get Current Shipping Status[/wpml-string]" output="bootstrap"]
<br>
[/wpv-filter-controls]
[wpv-filter-end]

Loop Editor

[wpv-layout-start]

	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop wrap="1" pad="true">
		[wpv-item index=1]
		<div class="row ">
			<br><div class="col-sm-12">[wpv-post-body view_template="Loop item in Search for Shipping Code"]</div>
		</div>
		[wpv-item index=other]
			<br><div class="col-sm-12">[wpv-post-body view_template="Loop item in Search for Shipping Code"]</div>
		[wpv-item index=pad]
			<br><div class="col-sm-12"></div>
		[wpv-item index=pad-last]
			<br><div class="col-sm-12"></div>
		</div>
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]

	[wpv-no-items-found]
		<br><strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]
#1880135

Hello,

I have tried the URL you mentioned above, after submit the search form, it pass wpv_post_search URL parameter to view.

I suggest you try these:
1) Modify your shortcodes as below:

...
[wpv-conditional if="( '[wpv-search-term param='wpv_post_search']' eq '' )"]
<strong class="input-missing">[wpml-string context="wpv-views"]You must enter a shipment code.[/wpml-string]</strong><br><br>
[/wpv-conditional]
...

2) Use the wpv_filter_query filter hook, for example, add below PHP codes into your theme file "functions.php":

add_filter( 'wpv_filter_query', function ( $query_args, $settings, $view_id ) {
    if ( $view_id == 999 && !isset($_GET['wpv_post_search'])) {
        $query_args['post__in'] = array(0);
    }
    return $query_args;
}, 10, 3);

Please replace 999 with your post view's ID

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#1882165

Thanks. The reason for including the wpv_post_search in the URL parameter is because this search form is also used on the home page and I don't want the "You must enter a shipment code." message on that page (obviously).

In the end, the easy way to solve this was simply by adding a conditional statement around the View Loop.

[wpv-layout-start]
[wpv-conditional if="( '[wpv-search-term param='wpv_post_search']' ne '' )"]

	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop wrap="1" pad="true">
		[wpv-item index=1]
		<div class="row ">
			<br><div class="col-sm-12">[wpv-post-body view_template="Loop item in Search for Shipping Code"]</div>
		</div>
		[wpv-item index=other]
			<br><div class="col-sm-12">[wpv-post-body view_template="Loop item in Search for Shipping Code"]</div>
		[wpv-item index=pad]
			<br><div class="col-sm-12"></div>
		[wpv-item index=pad-last]
			<br><div class="col-sm-12"></div>
		</div>
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]

	[wpv-no-items-found]
		<br><strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[/wpv-conditional]
[wpv-layout-end]

You can close the ticket. Thanks for your support.