Skip Navigation

[Resolved] Output View as JSON

This support ticket is created 8 years, 1 month 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.

This topic contains 2 replies, has 1 voice.

Last updated by Ryan 8 years, 1 month ago.


Hi there

I am trying to:
Output a views loop array into a JSON format

The file that needs to be output would be something like hidden link, but this isn't necessary; ultimately we want to achieve something like the code below using:

$args = array(
    'title' => 'My View name',
    'myattribute' => 'myvalue'
echo render_view( $args ); 

PHP loop reference from: hidden link

$db    = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
$start = $_REQUEST['from'] / 1000;
$end   = $_REQUEST['to'] / 1000;
$sql   = sprintf('SELECT * FROM events WHERE `datetime` BETWEEN %s and %s',
    $db->quote(date('Y-m-d', $start)), $db->quote(date('Y-m-d', $end)));

$out = array();
foreach($db->query($sql) as $row) {
    $out[] = array(
        'id' => $row->id,
        'title' => $row->name,
        'url' => Helper::url($row->id),
        'start' => strtotime($row->datetime) . '000',
        'end' => strtotime($row->datetime_end) .'000'

echo json_encode(array('success' => 1, 'result' => $out));

I visited these URLs:
hidden link

Thanks in advance!


Also, here is our Views Loop, where the following codes would equate to their corresponding variables in the above, pure php-based loop:

[wpv-post-id] = 'id'
[wpv-post-url] = 'title'
[wpv-post-title output="sanitize"] = 'url'
[types field="comm-calendar-event-type" output="raw"][/types]
[types field="comm-calendar-event-start" output="raw"][/types] = 'start'
[types field="comm-calendar-event-end" output="raw"][/types] = 'end'

	[wpv-items-found]<!-- wpv-loop-start --><wpv-loop>{[wpv-post-id output="raw"],[wpv-post-url output="raw"],[wpv-post-title output="sanitize"],[types field="comm-calendar-event-type" output="raw"][/types],[types field="comm-calendar-event-start"  output="raw"][/types]000,[types field="comm-calendar-event-end"  output="raw"][/types]000},</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>

And this is the 'basic' php file in our theme for which we would want to json_encode:


include '../../../wp-load.php';
$args = array(
	'title' => 'Calendar View',
$calendar_loop = array(render_view( $args )); 

echo json_encode(array('success' => 1, 'result' => $calendar_loop));


So far this is outputting:

{"success":1,"result":["\n\n\n\n\t\n\t9,http:\/\/localhost\/CALENDAR\/comm-calendar-event\/test-event-2\/,Test Event 2,event-info,1438819200000,1439510400000,8,http:\/\/localhost\/CALENDAR\/comm-calendar-event\/test-event-1\/,Test Event 1,event-warning,1438646400000,1438819200000,\n\t\n\t\n\n"]}

I'm just not sure why 1) the '\n\n\n\n\t\n\t' are appearing and 2) why I can't get the '{ }' to wrap around each loop item. I guess my question is now: how do I make an array out of render_view( $args )


EDITED: Alternatively, using the following code, I can get a basic query to output into JSON format correctly (outside of a Toolset view)

$args = array( 
	'post_type' => 'comm-calendar-event', 
	'post_status' => 'publish', 
	'nopaging' => true 
$query = new WP_Query( $args );
$posts = $query->get_posts();

$output = array();
foreach( $posts as $post ) {

	$output[] = array(
		'id' => $post->ID,
		'title' => $post->post_title,
		'url' => get_permalink($post->ID),
		'class' => get_post_meta($post->ID, 'wpcf-comm-calendar-event-type', true),
		'start' => get_post_meta($post->ID, 'wpcf-comm-calendar-event-start', true) . '000',
		'end' => get_post_meta($post->ID, 'wpcf-comm-calendar-event-end', true) . '000',
echo json_encode(array('success' => 1, 'result' => $output));

I'm still curious to know if arriving at a similar solution will work within Toolset, though 🙂