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.
Our next available supporter will start replying to tickets in about 1.48 hours from now. 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 7 years, 3 months ago.
Assisted by: Beda.