0

Hi everyone I'm having trouble filtering a custom post type "Resources" using Ajax. I'm trying to get the posts to filter via two taxonomies and then activating the Ajax call on a click event on a submit button. I keep getting a '0' response and not sure what I'm missing. If anyone could help me out that would be so much appreciated! Cheers, Sanny

Here's my jQuery:

    function filterResources(){
        var the_topic_filter = $('#the_topic_selecter').val();
        var the_resourcetype_filter = $('#the_resourcetype_selecter').val();

        $('#results').html('');
        $('#results').html('<tr><td colspan="5" class="thinking"><img src="/wp-content/themes/limenetwork/images/ajax-spinner.gif" /></td></tr>');

        // AJAX CALL
        var data = {
            action: 'resourcehub_filter',
            send_the_topic_filter : the_topic_filter,
            send_the_resourcetype_filter : the_resourcetype_filter,
        };

        // RESPONSE
        jQuery.post(ajaxurl, data, function(response) {
            $('#results').html('');
            $('#results').html(response);

            if(!$.trim(response)){
                $('#results').html('<tr><td colspan="5" class="thinking">We could not find any resources, please try again.</td></tr>');
            }

        });

    }

And here is my functions.php

// RESOURCE HUB FILTER

add_action( 'wp_ajax_resourcehub_filter', 'resourcehub_filters' );
add_action( 'wp_ajax_nopriv_resourcehub_filter', 'resourcehub_filter' );

function resourcehub_filter() {

    // GET VARS

    $the_topic_filter = $_POST['send_the_topic_filter'];
    $the_resourcetype_filter = $_POST['send_the_resourcetype_filter'];

    // LOOP ALL RESOURCES  (filter)
    global $post;

    // FOR NO FILTERS SELECTED

    if( $the_topic_filter == "" && $the_resourcetype_filter == ""){

        $args = array(
            'post_type' => 'resource',
            'post_status' => 'publish',
            'posts_per_page' => 10,
            'orderby' => 'date',
            'order' => 'DESC',
        );

    } else {

        $args = array(
            'post_type' => 'resource',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'orderby' => 'date',
            'order' => 'DESC',

            'tax_query' => array(

                'relation' => 'OR',
                array(
                    'taxonomy' => 'topic_tax',
                    'field'    => 'slug',
                    'terms'    => array( $the_topic_filter )
                ),
                array(
                    'taxonomy' => 'resource_tax',
                    'field'    => 'slug',
                    'terms'    => array( $the_resourcetype_filter )
                ),

            ),
        );
    }

    $myposts = get_posts( $args );

    foreach ($myposts as $post) : start_wp(); ?>

        <tr>
            <td><a href="<?php the_field('link'); ?>"><?php the_title(); ?></a></td>
            <td><?php the_title(); ?></td>
            <td>
                <!-- GET ACF TAXONOMY -->
                <?php

                    $terms = get_field('topic');

                    if( $terms ):
                    foreach( $terms as $term ):

                         echo $term->name; echo ' ';

                     endforeach;

                endif; ?>

            </td>
            <td><span class="new badge" data-badge-caption="

                    <?php
                        $terms = get_field('resourcetype');
                        if( $terms ):
                            foreach( $terms as $term ):
                                echo $term->name;
                            endforeach;
                        endif;
                    ?>

            "></span></td>
        </tr>

     <?php endforeach;

    rewind_posts();

    wp_die();

}

And I added Ajax like this:

// ADD AJAX TO WORDPRESS HEADER
add_action( 'wp_head', 'my_ajaxurl' );
function my_ajaxurl() {
    $html = '<script type="text/javascript">';
    $html .= 'var ajaxurl = "' . admin_url( 'admin-ajax.php' ) . '"';
    $html .= '</script>';
    echo $html;
}
  • The start_wp(); function has been deprecated since version 1.5. Use setup_postdata( $post ); instead. – Jack Johansson Aug 30 '17 at 5:38
0

When i want to send html in ajax i do:

ob_start();
//here are echo that makes content 
echo '<p>Test</p>'
$response = ob_get_clean();

wp_send_json($response);//die is included 

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.