Problem:
I need to programmatically get the results of a View and/or check if the View even returns some results.
Solution:
Views is a tool that is thought to display data on the Front End of a Website, using ShortCodes.
You can, of course, show a View anywhere you want, by using PHP.
We provide a Function to display a View with PHP:
render_view()
This allows you to display a View with PHP.
To check if a View returns any results at all, you can use the function
get_view_query_results()
An example:
- If a View returns no results, it will produce:
array(0) { }
Hence you can check:
if (empty (get_view_query_results(12))) { echo "view has no results"; //or anything else, as example a Custom Text }
and
if (!empty (get_view_query_results(12))) { echo "view has results"; //or anything else, as example View results }
To return the exact amount of posts a view returns:
$filtered_posts = get_view_query_results( 12 ); $amount = count($filtered_posts); var_dump($amount);
Then you can do things like:
if ($amount < 5) { echo "less than five"; } if ($amount == 4) { echo "4"; }
These are Code samples and need to be adapted. They do not extend Toolset functionality, but allow you to extract data, and check it, with Custom Code.
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-api/#render_view
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
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 2 replies, has 2 voices.
Last updated by Jeffrey 6 years, 8 months ago.
Assisted by: Beda.