0

I have been trying to create dynamic sorting of posts on the blog on click on some icons. Posts are custom post type and the have custom taxonomy.

The objective is:
On click on category name (of custom taxonomy) show (append) posts of that category below.

The problem is:
WP_Query does not accept the arguments.
The result of success function are WP_Query arguments, not html objects or anything usable on the js side.

The flow:
I registered and localized the script:

wp_enqueue_script( 'ajax-pagination',  get_stylesheet_directory_uri() . '/library/js/ajax-pagination.js', array( 'jquery' ), '1.0', true );



wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
                'ajaxurl' => admin_url( 'admin-ajax.php' )
 ));

On the server side I do:

add_action( 'wp_ajax_nopriv_ajax_pagination', 'my_ajax_pagination' );
add_action( 'wp_ajax_ajax_pagination', 'my_ajax_pagination' );

function my_ajax_pagination() {
    $name = $_POST['termname']; 
    $args = array(
        'post_type' => 'myposttype',
        'tax_query' => array(
            array(
                'taxonomy' => 'mytaxonomyname',
                'field' => 'slug', 
                'terms' =>  $name 
            )
        )
    );
    $posts = new WP_Query( $args );
    $GLOBALS['wp_query'] = $posts;


    if( ! $posts->have_posts() ) { 
        echo "I don't work yet";
    }
    else {
        while ( $posts->have_posts() ) { 
            $posts->the_post();
            $text = apply_filters('the_excerpt', 
            get_post_field('post_excerpt', $post_id));
            echo $text;
        }
    }


    die();
}

In the ajax file I go with:

(function($) {


    function get_the_category(element){
        return element.context.id;
    };

    $(document).on( 'click', '.cat-icons a', function( event ) {
        event.preventDefault();

        var termname = get_the_category( $(this).clone() );

        $.ajax({
            url: ajaxpagination.ajaxurl,
            type: 'post',
            data: {
                action: 'ajax_pagination',
                termname: termname
            },
            success: function( html ) {
                console.log(html);
                //here will do stuf
            }
        })
    })
})(jQuery);

Yes, I tried to look that up, no I haven't found the answer and I would like to know what am I doing wrong. I'm new to AJAX and custom post types.

Questions:
1. How to achieve the objective with this approach?
2. Is the approach correct & safe?
3. How does that even happen I get $args back?
4. I tried to achieve the same with query_vars but failed. I couldn't get tax_query attached to the query_vars.

Many thanks.

0

I don't know why you would see WP_Query arguments in the data returned, a copy/paste of your code here (with a few adjustments- post type, taxonomy, and term name) does not produce that result.

Getting the post excerpt via get_post_field will only work if you explicitly entered an excerpt in that field, it will not generate one from post_content. You've also not defined $post_id, but that shouldn't matter as it will fall back to the current $post.

Here's a version with the unnecessary bits removed, and some slight adjustments:

function my_ajax_pagination() {
    $name = $_POST['termname']; 
    $args = array(
        'post_type' => 'myposttype',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'mytaxonomyname',
                'field' => 'slug', 
                'terms' =>  $name 
            )
        )
    );
    $posts = new WP_Query( $args );

    if( ! $posts->have_posts() ) { 
        echo "I don't work yet";
    }
    else {
        while ( $posts->have_posts() ) { 
            $posts->the_post();
            echo get_the_excerpt();
        }
    }

    die();
}

One other change I would suggest is to put your post data into an array and return it via wp_send_json rather than echo data and markup directly.

  • Thank you! I actually messed up the structure in the real code and I noticed after I edited it for this question upload. I looked into wp_send_json as you suggested to improve it and it works well. Thanks a lot. – magdazelena Jul 4 '17 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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