Skip Navigation

[Resolved] Views Result within a result

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

Problem: I would like to get the value of a custom User field in PHP, and use that value in the Query Filter shortcode arguments of a View. Then I would like to use the results of that View in the shortcode arguments of another View. I'm using types_render_usermeta and render_view, but the results are not what I expect.

Solution: The APIs types_render_usermeta and render_view are designed to output content, and can include extra markup and syntax that will not work correctly in filters. Instead of using types_render_usermeta to get the value of a custom field, use the WordPress API get_user_meta, since it will return the raw value without any extra markup. Instead of relying on the results of render_view as an argument, access the data more directly using get_view_query_results.

Relevant Documentation:
https://codex.wordpress.org/Function_Reference/get_user_meta
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 6 years, 7 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 2 replies, has 2 voices.

Last updated by Christian Cox 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#759268

I am trying to get a result from a view (id 1081) that supplies me with a result 1120 then I want to use that 1120 Result in the next view as the attribute

add_shortcode('daily-pest', 'facilty26');
function facilty26($atts) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$owned = types_render_usermeta( "owned", array( "user_id" => $user_id, "output" => "raw" ) ); -- Finding out what Facility They Own
$output = array('id' => '1081', 'facility' => $owned); --- Using the Facility owned attribute to find latest report filled out
$issue = render_view( $output );
$args = array('id' => '969', 'report' => $issue); -- Trying to use the Result from The render_view( $output ); as the attribute to now ONLY list retults from the parent with the id that matched the result.
return render_view( $args );
}

I hope this make sense but i am stuck I can get the first render view to return the value that I want.. but can't get the second to work correctly...

#759404

To add to this... The Second Array Parameter is used to sort by Parent. If that helps any

#763701

Hi, I think instead of rendering Types fields and rendering Views, you should consider accessing the data values more directly.

$owned = types_render_usermeta( "owned", array( "user_id" => $user_id, "output" => "raw" ) ); 

Use get_user_meta instead of types_render_usermeta. The API types_render_usermeta just adds an unnecessary step, because you're using the raw data anyway:

$owned = get_user_meta($user_id, "wpcf-owned", true);

Documentation for get_user_meta here: https://codex.wordpress.org/Function_Reference/get_user_meta

$issue = render_view( $output );
$args = array('id' => '969', 'report' => $issue); 

When you call render_view, the results will include HTML markup, line breaks, and spaces that will cause problems in the second View's filter. Instead, you could use the API get_view_query_results to get the result set, then get the data you need from the results:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results

Let me know if you have questions about that.