Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got a js file like this:

var category = [];
var genre = [];
var type = [];
var mainContent = jQuery('#content');
var siteURL ="http://" + top.location.host.toString();    
var URL = siteURL + "/?category="+ category +"&genre=" + genre +"&type="+ type +" #content";

jQuery('.ajax-cb').each(function() {
    jQuery(this).click(function() {         
        mainContent.load(URL,function(){                    
            mainContent.animate({opacity: '1'});
        });                   
    });     
}); 

It does load GET[] parameters to change the loop,

in the theme's function.php I've got:

function pre_selected_results() {
  $taxquery = array(
      'relation' => 'OR',
      array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => $_GET['category'],
        'operator'=>'IN'
      ),
      array(
        'taxonomy' => 'genres',
        'field' => 'slug',
        'terms' => $_GET['genre'],
        'operator'=>'IN'
      ),
      array(
        'taxonomy' => 'types',
        'field' => 'slug',
        'terms' => $_GET['type'],
        'operator'=>'IN'
      )                                         
  );
  $stack[] = "dog";
  $the_query = new WP_Query($myquery);
  while ($the_query->have_posts()) : 
  //the loop

  //I then collect info from the loop displayed          
  $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);
      }
  endwhile;
/*And I use wp_localize_script(); to send $stack back to js*/
  wp_enqueue_script( 'feedback' );
  wp_localize_script( 'feedback', 'jsdata', $stack);
}; //end of pre_selected_results

On my home page I call pre_selected_results(); the loop display fine and refresh,

but $stack only refresh if I edit the URL with GET[] manually in the browser,

otherwise it just return 'dog', anything obvious to you that I'm missing?

share|improve this question
    
Im not sure this should really be tagged as url rewriting as there's no permalink modification here, if there is can you post the code? Can you rephrase your question too it's not clear what you're asking, and your code is incomplete ( you only posted JS, with no context html or generating PHP ) –  Tom J Nowell Jan 22 '13 at 10:17
    
I've done my best, thanks –  daco.marseille Jan 22 '13 at 10:43
    
can you include more? Including the wp_localize_script part, the pre_get_posts filter hook and the surrounding code to provide context? Anything you refer to you should include –  Tom J Nowell Jan 22 '13 at 10:58
    
Here we go...Thanks –  daco.marseille Jan 22 '13 at 11:52
    
And the feedback script, you also call mainContent.load() and mainContent.animate() but don't show it being created/acquired, is it a jquery object? =s You're also not showing where pre_selected_results is called, be it a filter etc, your code is incomplete. Show all complete code. –  Tom J Nowell Jan 22 '13 at 13:32
show 8 more comments

1 Answer 1

You should use the WP AJAX APIs rather than reinventing them in your home template, e.g.:

functions.php :

// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

add_action('wp_ajax_my_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');

function my_action_callback() {
    $content = 'something to send back to the browser';

    echo $content;

    die(); // this is required to return a proper result
}

Javascript:

jQuery.post(
    // see tip #1 for how we declare global javascript variables
    MyAjax.ajaxurl,
    {
        // here we declare the parameters to send along with the request
        // this means the following action hooks will be fired:
        // wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
        action : 'myajax-submit',

        // other parameters can be added along with "action"
        postID : MyAjax.postID
    },
    function( response ) {
        alert( response ); // 
    }
);

Using this you can bypass your broken code and rely entirely on standardised APIs

You can read more about it here:

http://codex.wordpress.org/AJAX_in_Plugins

It also applies to themes

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.