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
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
That worked perfectly. Thank you so much! As always, your team is the best.
My issue is resolved now. Thank you!