Skip Navigation

[Resolved] Conditional HTML based results returned by a View

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

Problem:
I want to use a view in a HTML conditional, so that I can display or hide stuff, when the view has or has no (or an amount of) results.
How to do that?

Solution:
The cleanest way to do this is create a custom application using the View's API and some PHP:
https://toolset.com/forums/topic/conditional-inside-content-template-if-two-views-loop-is-nothing-found/#post-1323567

You could use the View "Disable the wrapping DIV around the View" option, which would allow you to return a raw loop, and use that in an HTML condition, but the problem is that this option ONLY applies to what's WITHIN the loop of a View, and the TOTAL of posts found, needed in this case, cannot be inserted INSIDE the loop.

Hence you need to use the custom code solution above for this case.
If you where to conditionally check LOOP output of a View, you could use the "Disable the wrapping DIV around the View" and neglect any custom code.

This support ticket is created 4 years, 8 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 3 replies, has 2 voices.

Last updated by Beda 4 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#1323469

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!!

#1323567

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.

#1323745

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.

#1324943

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.

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