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

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

1 Answer 1

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);
share|improve this answer
    
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 '11 at 11:17

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.