Skip Navigation

[Resolved] Use json data as variable in views filter

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

Problem: I have a View that filters based on shortcode attributes. I would like use a JavaScript templating library like Handlebars to set the View's shortcode attribute with data from a JSON object that exists on the page when the page loads.

Solution: Unfortunately there is no JavaScript API for forms, so there is not an easy way to set a shortcode attribute value with JavaScript after the View has loaded. If the JSON data is available in PHP before the page loads, then you may be able to use the PHP API render_view to set the desired attribute value and render the View programmatically instead of using the wpv-view shortcode:

$args = array(
    'name' => 'grab-categories',
    'billid2' => 'some value'
);
echo render_view( $args );

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

This support ticket is created 4 years, 12 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 briana-5 4 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1430631

I am trying to create a view that filters posts based on a shortcode attribute. The attribute to be provided is supplied from json data that is loaded on the page. I'm using handlebars.js to render a template that displays some of the data. I then want to say "where the value from this json object equals the value of shortcode attribute/field, display the view"

So the resulting code looks like this:

[wpv-view name="grab-categories" billid2="{{billId}}"]

The problem appears to be that the handlebars template does not render before the view evaluates the shortcode, resulting in it passing the markup (I.e. '{{billId}}') rather than the value. Which isn't surprising. I've tested the view by supplying an actual value instead of markup and the view works.

So I guess my question is, is there a way to do what I'm trying to achieve with Views? Is there a way, for example to ensure that it evaluates the value not the handlebars markup? Can you suggest another way to use the value of a json object as the value in the shortcode?

#1431611

Well there is no JavaScript API for Views, so there's no built-in way to achieve JS-based filtering from the front-end of the site. The results are pre-determined before the page loads, so obviously front-end code will not influence the initial View results. If you have access to the JSON object in PHP before the page loads, you can use the Views API instead of the View shortcode to render the View. Instead of including billid2 in the shortcode attribute, you would include it in the args array. We have documentation about the render_view API available here:
https://toolset.com/documentation/programmer-reference/views-api/#render_view

For example:

$args = array(
    'name' => 'grab-categories',
    'billid2' => 'some value'
);
echo render_view( $args );
#1435033

Thanks. I’ll dig in on that approach.