Skip Navigation

[Resolved] Shortcode to check if a view has output

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

Problem: I need to know if a View will have any results

Solution: A custom shortcode can be used to count the results of any View:

add_shortcode( 'ts_view_has_results', 'ts_view_has_results_func');
function ts_view_has_results_func($atts)
{
  $view_id = intval($atts['viewid']);
  $results = get_view_query_results( $view_id );
  return count($results);
}

Use the shortcode - replace 123 with the View ID of your choice:

[ts_view_has_results viewid="123"]

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

This support ticket is created 7 years, 6 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
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)

This topic contains 4 replies, has 2 voices.

Last updated by nabils 7 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#516153

Dear,
Here my problem:
I have a custom type "registration" and each user can create one single "registration".
In another content template (of another custom type "competition"), I have to show up some content only if the current user has a registration. So basically, and if I am thinking right, I need a shortcode which checks is the current user has a "registration" (where he is the author). In other words, I need a shortcode to check if a view (which outputs the registrations of the current user) has an empty output or not.

Any clue please?
Regards
nabil

#516249

Hi, I don't think you need a special shortcode to do this. I think you can accomplish this with a fairly simple View. Create a new View that will display registrations for the current User. In your Loop Output, you can place whatever content you want to include for Users with registrations in the <wpv-loop> area. You can place whatever content you want to include for non-registered users in the "No items found" area. Finally, place this View in your content template for competitions.

Let me know if I can help clarify this approach.

#517206

Thank you Christian,
I am aware of this solution but actually it is my fault, I did not give you enough information in my first request. When I said " I have to show up some content only if the current user has a registration", the "some content" I meant here is actually a child CRED to create a signup to the current competition (this signup is a child of competition).
If I put this cred link in the registration view, I will loose the link between the current competition (father) and the signup (child). So I think I need a shortcode to check if a view has an empty output or not.

#517488

Here's a shortcode that will return the number of results for any View:

add_shortcode( 'ts_view_has_results', 'ts_view_has_results_func');
function ts_view_has_results_func($atts)
{
  $view_id = intval($atts['viewid']);
  $results = get_view_query_results( $view_id );
  return count($results);
}

Use the shortcode - replace 123 with the View ID of your choice:

[ts_view_has_results viewid="123"]

You can use the value returned by this shortcode in a conditional if you register it as described here:
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

#518677

Thanks,
This is exactly what I need.