1

I'm using WP_Query to collate some info about posts based on their custom taxonomy. My arguments are as follows:

$args = array(
        'posts_per_page'=> -1,
        'post_status' => 'publish'
    );

    if ( isset($_POST['tag_id']) ) :
        $args['tax_query'] =
            array(array(
                'taxonomy' => 'region',
                'field' => 'id',
                'terms' => $_POST['tax_id'],
            ));
    endif;

Then I loop through and collate my data. And that works perfectly well. But because it's quite a heavy query, and appears for each taxonomy id I thought I'd run it on user demand and return the data with wp_ajax_

The same query seems to ignore all the tax_query arguments when returned through ajax. The arguments are the same and the correct "terms" is being passed. It's identical. I can't figure it out. Is this a bug? It's bugging me.

Updated to put the code in pastebin: http://pastebin.com/rxSJ1C2n

The class is at the top, then the bit of jQuery I've put at the bottom but is actually in a separate file.

2
  • Without seeing a pastebin (or similar) of your whole ajax code, it's just guessing.
    – kaiser
    Oct 25, 2011 at 18:12
  • I've put it in pastebin now. Thanks for the suggestions.
    – deadlyhifi
    Oct 25, 2011 at 18:50

1 Answer 1

3

I don't see your call to the ajaxurl in your code. Are you actually getting an ajax response?

Try this javascript:

jQuery(document).ready( function($) {
    $("a.getviews").click( function() {
        var td = $(this).parent();
        /* only fetch results once */
        $(this).unbind('click').bind('click', function(){return false;});
        // replace button with loader
        $(td).html('<span class="loader"></span>');

        $.ajax({
                type: 'POST',
                url: ajaxurl,
                data: { action: "get_views", tax_id: td.attr("id") },
                success: function(response) {
                $("td").html(response);
                return false;                               
            }

            });

            });
    });

With large ajax responses I always have better luck buffering the output.

Add ob_start(); before your loop then at the end add:

$response = ob_get_contents();
                ob_end_clean();

            echo $response;

                die(1);
1
  • Thanks for your answer, it gave me a few pointer - particularly to learn about ob_start(). However, it was all because of an error in my code. I'd been using tag_id in PHP and tax_id in JS.
    – deadlyhifi
    Oct 31, 2011 at 11:17

Your Answer

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

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