Skip Navigation

[Resolved] get views results and check if has results

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

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 support ticket is created 7 years, 3 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.

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 Jeffrey 7 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#559090

I am using conditional output for checking how many items has been submitted by current user , I am thinking about checking value of specific custom fields, or checking with view shortcode. I guess that should be wpv-found-count or wpv-items-count if using view shortcode.

I am going to use checking results for reminding current user how many forms has been completed, and how many is incomplete , that will be shown on the top of user back end , it is different with view content, so I can not use [wpv-filter-meta-html][wpv-layout-meta-html] for displaying information about. my question is how to code for the example I have below?

for example

the name of view: Room list; slug room-list

I just want to remind current user if no found, show "please submit your first ad with a link", if item greater than or equal 1, show the count of item(s)

Thank you so much for taking you time, I am a Wp beginner. I do not know coding at all, so if possible please leave a piece of code according to my condition as well.

#559140

I mentioned this earlier already.
It is not possible with Toolset features to check if a CRED form has been submitted, unless:
- check on Fields in the Database (or any other data) that this Form submits
- use Custom Code:
https://stackoverflow.com/questions/7711466/checking-if-form-has-been-submitted-php

I am not sure what approach you use, but with Toolset alone, this is not possible.

If you check on whether or not a certain field exists, then this works, but it means you check if data exists, strictly speaking, and not if a CRED form has been submitted.
The user could create that data in the backend, right?

The wpv-found-count or wpv-items-count ShortCodes can be used only in a View Loop.
They do this:
wpv-found-count:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-found-count

Displays the total number of posts, users or taxonomies that have been found by the Views query.
==> This value is calculated before pagination, so even if you are using pagination, it will return the total number of posts matching the query.

wpv-items-count:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-items-count

description:
Outputs the number of posts, users or taxonomies that will be displayed on the page.
==> When using pagination, this value will be limited by the page size and the number of remaining results.

Regarding this:

that will be shown on the top of user back end

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()
https://toolset.com/documentation/programmer-reference/views-api/#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()
https://toolset.com/documentation/programmer-reference/views-api/#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.

#561562

Thank you so much Beda, It seems too difficult to me, I may have to think about my case again!