Site icon Hip-Hop Website Design and Development

AJAX response showing as 0

I am trying to update the contents of the #documents element to be that of the return from my get_documents_by_year action which is sorted by a year which is determined on the ‘year-button’ clicked.

However, I seem to be receiving a response of 0 even though all of my code is correct. The only difference between this and my other projects is that this project has been built using Sage by Roots. So I was wondering if that may have anything to do with it, though I was unable to find anything on the web.

I’ve looked at similar posts from others, but their solutions haven’t worked for me.

ajax.php

add_action( 'wp_ajax_nopriv_get_documents_by_year', 'get_documents_by_year' );
add_action( 'wp_ajax_get_documents_by_year', 'get_documents_by_year' );

function get_documents_by_year() {
    $year = $_POST['year'];
   $taxonomy = $_POST['taxonomy'];

   $args = array(
      'post_type' => 'document',
      'post_status' => 'publish',
      'tax_query' => array(
         array(
            'taxonomy' => 'document-type',
            'field' => 'slug',
            'terms' => array($taxonomy)
         )
      ),
      'year' =>  $year,
      'posts_per_page' => -1
   );
    

    $documents = new WP_Query( $args ); 

   if ( $documents->have_posts() ) {
      while($documents->have_posts()): $documents->the_post();
         echo Apptemplate('partials.content-document' );
      endwhile;
   }

   die();
}

get_documents_by_year.js

This file works fine and does what I expect it to do.

/* eslint-disable no-undef */

   jQuery('.year-button').on('click', function(event) {
       event.preventDefault();

      const year = $(this).data('year');
      const taxonomy = $(this).data('taxonomy');
      const $results = jQuery('#documents');
   
      jQuery.ajax({
          type : 'post',
          url : myAjax.ajaxurl,
          data : {
              action : 'get_documents_by_year',
              year: year,
              taxonomy: taxonomy,
          },
          beforeSend: function() {
              $results.addClass('loading');
              console.log('loading');
          },
          success : function( response ) {
              $results.removeClass('loading');
              $results.html( response );
          },
          error: function (errorThrown) {
              console.log(errorThrown);
          },
      });
      
      return false;
   });

setup.php

add_action('wp_enqueue_scripts', function () {
    wp_enqueue_style('sage/main.css', asset_path('styles/main.css'), false, null);
    wp_enqueue_script('sage/main.js', asset_path('scripts/main.js'), ['jquery'], null, true);

    wp_enqueue_script( 'get_documents_by_year_script', asset_path('scripts/get_documents_by_year.js'), ['jquery'], time(), true);
    wp_localize_script( 'get_documents_by_year_script', 'myAjax', ['ajaxurl' => admin_url( 'admin-ajax.php' )]);       

    if (is_single() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }
}, 100);