0

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?

11
  • 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 ) Commented Jan 22, 2013 at 10:17
  • 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 Commented Jan 22, 2013 at 10:58
  • 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. Commented Jan 22, 2013 at 13:32
  • I did edit the js file, showing what maincontent is,I've been working for weeks on it, so I can't paste the whole code here there is too much code, the pre_selected_results(); is called from my home page instead of the loop, no filter set. thanks Commented Jan 22, 2013 at 13:46
  • there are always pastebins and gists, exactly what are you trying to achieve with this code? It's necessary to know what the correct output/behaviour would be if we're to diagnose the incorrect output/behaviour Commented Jan 22, 2013 at 13:50

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.