Ajax Post Filter with WP_Query only fires once with Category
<p>I have an ajax post filter modeled after <a href="https://rudrastyh.com/wordpress/ajax-post-filters.html" rel="nofollow noreferrer">this article</a>. I have it working mostly as desired but I wanted to be able to filter lectures by a category (it is actually a sub-category but I don't know if that makes any difference). </p>

<p>I have filters for speaker and for the latest lectures and those work fine, but when I try to run the Series filter (the part with elseif( isset( $_POST['series_name'] ) ) ) I get no response. If try running the Series filter first it will fire once but then will not respond. If I try running any other filter first they will work fine but then the Series filter will not fire at all. I think my problem is how I'm handling the $args for the WP_Query for when $_POST['series_name'] is set.</p>

<p>Any pointers would be greatly appreciated, thanks!</p>

<p>Here is my php:
    

<pre><code>function lectures_filter_function(){
$i = 1;
$args = array( 
    'post_type' =&gt; 'lectures',
    'orderby' =&gt; 'meta_value',
    'meta_key' =&gt; 'lecture_date',
    'order' =&gt; 'DESC'       
); 

if( isset( $_POST['speaker'] ) ) 
{
    $args['meta_query'][] = array(
        'key' =&gt; 'speaker',
        'value' =&gt; $_POST['speaker'],
    );
} 
elseif( isset( $_POST['series_name'] ) ) 
{
    //*This is how I wanted to code it but it didn't work at all*
    // $args['tax_query'] = array(
    //      array(
    //          'taxonomy' =&gt; 'category',
    //          'value' =&gt; 'category_name',
    //          'terms' =&gt; $_POST['series_name']
    //      )
    //  );

    //This is how I at least got it to fire once per screen load.
    $args = array(
                'category_name' =&gt; $_POST['series_name'],
                'post_type' =&gt; 'lectures',
                'orderby' =&gt; 'meta_value',
                'meta_key' =&gt; 'lecture_date',
                'order' =&gt; 'DESC'                   
        );      
}


    $the_query = new WP_Query( $args );
    $postlist = $the_query-&gt;post_count;

    // The Query

    // The Loop
    if ( $the_query-&gt;have_posts() ) {
        while ( $the_query-&gt;have_posts() ) {
            $the_query-&gt;the_post();

            // print_r($the_query);

            if($i % 2 == 1){
                echo '&lt;div class="row"&gt;';
            }   
        ?&gt;      

        &lt;div class="large-6 columns lecture-tile"&gt;
            &lt;div class="lecture-overlay-container"&gt;
                &lt;a href="&lt;?php the_permalink();?&gt;"&gt;
                    &lt;div class="lecture-tile-overlay"&gt;&lt;/div&gt;
                    &lt;?php the_post_thumbnail('full');?&gt;
                    &lt;div class="lecture-tile-info"&gt;
                        &lt;h3&gt;&lt;?php the_title();?&gt;&lt;/h3&gt;

&lt;?php 
    $current_id = get_the_id();
    $lecture_type = get_post_meta($current_id, 'lecture_type', true);
    if($lecture_type == "Single"){
        $lecture_descriptor = "lecture";
    }elseif ($lecture_type == 'Series') {
        $series_name = get_post_meta($current_id, 'series_name', true);
        $lecture_descriptor = $series_name;
    }elseif ($lecture_type == 'Event') {
        $event_name = get_post_meta($current_id, 'event_name', true);
        $lecture_descriptor = $event_name;
    }           
?&gt;                              
                        &lt;p&gt;&lt;?php echo $lecture_descriptor; ?&gt;&lt;/p&gt;                           
                    &lt;/div&gt;
                &lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;  

        &lt;?php
                if($i % 2 == 0 || $i == $num_posts){
                    echo '&lt;/div&gt;';
                }
                $i++;
        }
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        // no posts found
        echo "No matching lectures found...";
    }
die();
</code></pre>

<p>}</p>

<pre><code>add_action('wp_ajax_myfilter', 'lectures_filter_function'); 
add_action('wp_ajax_nopriv_myfilter', 'lectures_filter_function');
</code></pre>