get data from wp-query, outside the loop & without url change

It sounds like you’re looking for wp_localize_script() which lets you pass data to a script that is already enqueued.

It works roughly like this:

<?php    
// assuming your script is already registered
wp_enqueue_script( 'wpse_81817' );

// Do the array building of terms you plan on doing
$wpse_81817_phpdata = array( 'myKey' => 'a value' );

// Pass that to the JS
wp_localize_script( 'wpse_81817', 'wpse_81817_jsdata', $wpse_81817_phpdata );

Then, in your wpse_81817 script, you can get at the data like so:

print wpse_81817_jsdata.myKey;

That should return:

'a value'

Leave a Comment