Home › Toolset Professional Support › [Resolved] “Not found” message in nested View
Problem: I have two nested Views set up to display a list of child posts. If no child posts are found, I would like to display "No items found" once. However, if I add the "No items found" message in the child View, it is repeated for each parent post. If I add it in the parent View, it is never shown because there are always parent posts.
Solution: There is not a simple way to set this up because each child View is rendered independently of the other child Views. There is no built-in way to keep a running total of all child posts across the nested Views, so a custom code solution in the classic Views editor is required. You must use a custom shortcode to add the number 1 each time a child post is displayed. Then use a conditional to test the total amount after all the child post loops have run.
First, add these two custom shortcodes in your child theme's functions.php file, or in a new custom code snippet in Toolset > Settings > Custom Code:
// https://toolset.com/forums/topic/not-found-message/ global $total; function add_total_shortcode($atts, $content = '') { global $total; $total += wpv_do_shortcode($content); } add_shortcode('add-to-total', 'add_total_shortcode'); function show_total_shortcode( $atts, $content ) { global $total; $atts = shortcode_atts( array( 'decimals' => 0 ), $atts ); $totalNew = $total; $total = 0; return number_format($totalNew, (int) $atts['decimals'] ); } add_shortcode('show-total', 'show_total_shortcode');
Go to Toolset > Settings > Front-end Content and register show-total in Third-party shortcode arguments.
In your child View, insert the following code somewhere inside the wpv-loop tags:
[add-to-total]1[/add-to-total]
Then in the parent View, insert the following conditional shortcode somewhere outside the wpv-loop tags but inside the wpv-items-found shortcode:
[wpv-conditional if="( '[show-total][/show-total]' gte '1' )" evaluate="false"] There are NO child posts [/wpv-conditional]
You may replace the text with your own custom message as needed.
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
Related documentation:
This topic contains 5 replies, has 2 voices.
Last updated by nicolaS-3 4 years, 5 months ago.
Assisted by: Christian Cox.
Hi
I have two nested views working ok when posts exist. When no posts are found I can't manage to get the "not found" message properly.
View1 loops on parents and shows a table header. View2 loops on children (related to parents with a one-to-many relationship) and adds the table rows, showing children of parents using the filter "Select posts in a parent-children relationship that are a related to the current post in the loop".
If I put the "Not found" message in View1 within [wpv-no-items-found] it never displays because parents always exist. If I place it in View2 the message is repeated for each parent !
Is there a way to display the message in View2 just once, at the end of all loops ? maybe using wpv-conditional ?
I also would like to include a "Please wait" spinner, I tried in both views, but it never displays, is there any special condition to add for the spinner to show ?
Thanks
Regards
Nicola
New threads created by Christian Cox and linked to this one are listed below:
https://toolset.com/forums/topic/spinner-is-never-displayed/
Hi, I've split your spinner question into a separate ticket so you'll get faster support for both issues (someone else may get a chance to answer that question before I can answer both), and so we can keep the forum organized.
It sounds like you need a way to solve the "no results found" problem in nested Views. It seems like you would be able to programmatically count the number of child posts in all the nested child Views, and then test that total number in a conditional. If the total is 1 or greater, you know there are child posts. If the total is less than one, show your "no results found" message. Unfortunately it is not straightforward to count things or calculate running totals during a loop (much less across nested loops). Each child View is independent of the other child Views in the parent View, and there isn't a built-in way for each child View to know whether or not the previous child View had any results or communicate out to the parent View with that information. That means it's not possible to keep a running tally of results from one child View to the next using any of the built-in features. Instead, what you can do is use a custom shortcode that displays something like a "variable" - the value should be initialized at zero, and then it should increment by 1 for each iteration of the child View.
There are several tickets in the forum that use a custom shortcode to do something similar, but slightly more complex. This ticket involves calculating the sum total of custom numeric fields shown in a View, sort of like a spreadsheet: https://toolset.com/forums/topic/nested-view-with-conditional-sums/
In your case, we'll just add the number 1 in each iteration of the loop instead of adding the custom field value. Here's how that would look in your parent post View:
[wpv-conditional if="( '[show-total][/show-total]' gte '1' )" evaluate="false"] There are NO child posts [/wpv-conditional]
You would add the above code to your parent View somewhere after the wpv-loop tag closes, but before the wpv-no-items-found shortcode. Feel free to adjust the text as needed.
In your child post View, add the following code somewhere inside the wpv-loop tags:
[add-to-total]1[/add-to-total]
This code should be added in a location that is executed once and only once each time the loop runs. For example, avoid placing it in a conditional where it could be skipped. Remove the "no results found" text, since that is now handled in the parent View.
Finally, you need the PHP code that creates these shortcodes. Add the following code in your child theme's functions.php file, or create a new snippet in wp-admin > Toolset > Settings > Custom Code and add the code to the end of that snippet:
// https://toolset.com/forums/topic/not-found-message/ global $total; function add_total_shortcode($atts, $content = '') { global $total; $total += wpv_do_shortcode($content); } add_shortcode('add-to-total', 'add_total_shortcode'); function show_total_shortcode( $atts, $content ) { global $total; $atts = shortcode_atts( array( 'decimals' => 0 ), $atts ); $totalNew = $total; $total = 0; return number_format($totalNew, (int) $atts['decimals'] ); } add_shortcode('show-total', 'show_total_shortcode');
Let me know if you run into problems implementing this solution, or if I misunderstood what you want to achieve.
Hi Christian,
you perfectly got the issue, I didn't think I run into a known one ....
I believe I have implemented all according to your indications and placed the code in the right positions, but still the message isn't shown. Could you please take a look ?
thanks
Regards
Nicola
Or course, I can take a closer look. I am currently blocked from the site by Wordfence. Can you resolve that restriction? See the screenshot here for details. Also, please provide login credentials in the private fields here, and let me know where I should see the "No results found" message on the front-end of the site once the Wordfence restriction has been resolved.
Looks like I left out a step. You need to register show-total in wp-admin > Toolset > Settings > Front-end Content > Third party shortcodes so you can use this custom shortcode in conditional evaluations. I did that for you, and now I see the message when my list is empty - can you confirm?
My issue is resolved now. Thank you!