Tell us what you are trying to do?
Display a message if the results from two Views are empty
I have a content template. Inside this content template, I have two views.
I would like to show a message if the result of the loop on BOTH views is empty.
Thanks!!
You would best work with Custom PHP and the Views API here, it doesn't require a lot of code and is the cleanest way to approach the problem.
I elaborated on the same task recently, where I also provided a working sample code:
https://toolset.com/forums/topic/conditional-on-there-being-more-than-one-item-in-a-taxonomy-assigned-to-a-post/#post-1321835
Note, if you actually want to count the Views Results, you can use get_view_query_results().
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
With that function, you will get an array of resulting posts, which if count()ed can tell how many posts there are.
So you can use that function to create for example a Custom Shortcode that count()s it's output and return it
See how a ShortCode can be created here:
https://codex.wordpress.org/Shortcode_API
That ShortCode then needs to be registered in Toolset > Settings, as described here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes
Finally, this ShortCode, returning the amount of found posts as a number, can be used in the HTML condition, to check for example if is 0 or gt (greater than) 0, and output adequately messages or other code.
Please let me know if you need more help with this.
Thank you! With your help and some digging, I came up with a solution. I am not sure if this is the best or most effective solution, but it works.
What I did:
Created a shortcode which adds a count for each view using get_view_query_results :
// Checks for Posts
function get_job_post_count()
{
$active_job_posts = get_view_query_results(6483);
$num_active_posts = 0;
foreach ($active_job_posts as $active_job_post) {
$num_active_posts ++;
}
$inactive_job_posts = get_view_query_results(6485);
$num_inactive_posts = 0;
foreach ($inactive_job_posts as $inactive_job_post) {
$num_inactive_posts++;
}
return $num_active_posts + $num_inactive_posts;
}
add_shortcode('job-count-dashboard', 'get_job_post_count');
Now I have a number that tells me if each view has a count totaled for both.
Then I used the shortcode in my content template:
[wpv-conditional if='( "[job-count-dashboard]" eq "0")']My Message[/wpv-conditional]
I wish there was a 'native toolset' was to get empty for a view inside a content template.
Thanks again.
That is also a valid solution.
You can cut the entire foreach() however, since you only need to know if the View has 0 or X results.
You can just count() get_view_query_results() 🙂
It's an array, and count() will count the array items, which either are none (0) or well, the number of posts found
Saving 2 foreach() over potentially a high amount of posts, in potentially more than one place, can save a lot of server execution time, and hence speed the site up.