Skip Navigation

[Resolved] Get number of results for View filtered by shortcode attribute

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

Problem: I would like to use get_view_query_results to find the number of results for a View, filtered by a shortcode attribute.

Solution:
The 4th argument of the get_view_query_results function takes an array of arguments, like those you would add as View shortcode attributes. In order to pass that into the get_view_query_results function, you probably need to add it as an attribute in your custom shortcode like this:

function get_students_count( $atts )
{
    $atts = shortcode_atts( [
      'wpvprchildof' => 0
    ], $atts );
    $args = array( 'wpvprchildof'=> $atts['wpvprchildof'] );
    $filtered_posts = get_view_query_results(258, null, null, $args);
    return count($filtered_posts);
}
add_shortcode( 'wpv-students-count', 'get_students_count' );

Then use the shortcode like this:

[wpv-students-count wpvprchildof='[wpv-post-id]'][/wpv-students-count]

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

This support ticket is created 6 years 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 5 replies, has 2 voices.

Last updated by vimalS 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#630516

Tell us what you are trying to do : Display no of results returned by view in content template after all filters have applied.
I have tried [wpv-found-count] , But it is working inside view. I want to display it in content template.
I have tried creating shortcode as well but it is giving results without applying filter.

function get_students_count()
{
    $filtered_posts = get_view_query_results(258);
    return count($filtered_posts);
}
add_shortcode( 'wpv-students-count', 'get_students_count' );
#630613

I have tried creating shortcode as well but it is giving results without applying filter.
Can you share a screenshot of this View editor in wp-admin? I would like to see how the Query filter is configured. Please also tell me about the Content Template where you want to display this information. Is it the template for a CPT?

#630803
Screenshot_1.png
Screenshot_2.png

Yes, Please check attached screenshot.
Content template is not for any CPT.

#631092

I see, the View's query filter is configured to respond to a shortcode attribute, "wpvprchildof". If you want the filter to be applied, you must include the wpvprchildof attribute as an argument in the get_view_query_results function call. The 4th parameter accepts an array of arguments. To make the parent post ID dynamic, you probably need to pass it into the wpv-students-count shortcode as an attribute. Something like this:

function get_students_count( $atts )
{
    $atts = shortcode_atts( [
      'wpvprchildof' => 0
    ], $atts );
    $args = array( 'wpvprchildof'=> $atts['wpvprchildof'] );
    $filtered_posts = get_view_query_results(258, null, null, $args);
    return count($filtered_posts);
}
add_shortcode( 'wpv-students-count', 'get_students_count' );

Then use the shortcode like this:

[wpv-students-count wpvprchildof='[wpv-post-id]'][/wpv-students-count]

Obviously this only works if wpv-post-id represents the parent post's ID here. If it does not, then you must replace the wpv-post-id shortcode with something else that represents the parent post's ID.

#631868

Thanks Man, Its working

#641768

I have one more filter with this, which is Exclude posts with IDs set by the View shortcode attribute "ids" eg. [wpv-view name="view-name" ids="1"]. what I need to add in above code you gave?

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