Skip Navigation

[Resolved] Passing URL_PARAM arguments to 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Alex Russell 1 year, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2556033
Screenshot 2023-02-16 145059.png

Tell us what you are trying to do?

I have a CPT "Legislators" and a view that filters them. It accepts URL_PARAMs (attached image). It works just fine when I embed the view on a page, and filters the results as expected.

I have a script that outputs JSON data from this view, using get_view_query_results(), however I cannot get that function to accept my search parameters.

The expected result is to return only Legislators where state=Virginia. The actual result is it's returning all of the data without filtering by state, as you can see here: hidden link
[code]
<?php
/*
Template Name: Alex
*/
header('Content-Type: application/json');
include("../../../wp-load.php");

// Get the results of the Toolset Legislators-JSON view and output it as json.
$args = array(
'state' => "Virginia",
'wpcf-state' => 'Virginia'
);

$filtered_posts = get_view_query_results( 9627,null,null,$args);

foreach ( $filtered_posts as $filtered_post ) {
$key = $filtered_post->ID;
$meta = get_post_meta($key);
/** Do stuff with the data here **/
}
echo json_encode($returnArray);
?>
[/code]
Is there any documentation that you are following?

https://toolset.com/forums/topic/output-view-as-json/
https://toolset.com/documentation/programmer-reference/views-api/
https://toolset.com/forums/topic/big-problem-with-view-2/

What is the link to your site?
hidden link

#2556433

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

If the view's query filter is set to filter by the URL parameter(s) and the URL of the page doesn't have those values, you can set them in the code through the super global '$_GET'.
( ref: hidden link )

For example:


....
	// set 'district' URL parameter value
	$_GET['district'] = 'abc';
	// set 'state' URL parameter value
	$_GET['state'] = 'def';
	// set 'branch' URL parameter value
	$_GET['branch'] = 'ghi';
	$filtered_posts = get_view_query_results( 9627,null,null,$args);
....

Note: The method of passing the values through the '$args' array that your code currently has, will work if the view's query filter is set to use the 'shortcode attributes' and not the URL parameters.
( ref: https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results )

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2556625

That worked perfectly. Thank you so much! As always, your team is the best.

My issue is resolved now. Thank you!

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