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

I am working on an advanced search on wordpress based on custom taxonomies.
I’ve been stuck for 72h so I was hoping to have some help or thought…

Step 1 — in the js file the query strings are created like that:

Read More
if (jQuery('#s').val() == ''){
URL = "/?genre=" + genre + '...other Stuff' #content';
}else{
URL = "/?s="+searchQueryString+"&genre=" + genre +'...other stuff' #content';
}

It nicelly load my custom loop in my #content div without changing the browser url or reloading the header, which is pretty good…so far. 🙂

Step 2 — then I wrote 2 functions in my function.php , one to load the loop with the GET[] elements on main page, using new WP_Query
and one that does the same thing for search queries but only filter with:

add_action('pre_get_posts','SearchFilter');

Which is compiling my GET[] filters with the GET[s] in the content.php,
Still all good….

Step 3 — (problem^^)—

I want to add a css class to desactivate the radio buttons located in my header.php, depending on the results in the loop.

Try-1 I thought I could create a php array to compile the terms found while the loop is happening, and then compare it with my buttons value.like that:

$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slugs');
$results = wp_get_post_terms(get_the_ID(),'category',$args);  
foreach ($results as $result){
    array_push($stack, $result);

}

But there is no way to retrieve the data from that array in the header afterwhile, or to create it from the header using things like global $post;since my url doesn’t change.
it just shows the homepage query.

Try-2 I also thought I could encode it to json and then put some action in my js file. but so far it just return json unexpected character, and I got the feeling that even if I crack it, its not going to be the right way since its going to make the js file heavier.

May be I’m just missing something about the Global wp_query and I don’t need to charge my script?

Excuse my english and the long question,

thanks a lot in advance if you have an idea,

DACO

Related posts

Leave a Reply

1 comment

  1. 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'