Ajax calls from the theme directory

Question

I’ve got a custom theme I build using an underscores starter theme. Inside one of my template files (page-talent.php) I’ve got this jQuery ajax function:

   $.ajax({
          type: 'POST',
          url: '<?php echo admin_url('admin-ajax.php');?>',
          dataType: "html", // add data type
         // data: { action : 'get_ajax_posts' },
          data: { action : 'get_ajax_posts' , filters: filters },
          success: function( response ) {
              console.log( response );
              //alert(response);
              $( '.posts-area' ).html( response ); 
          }
      });      
    })

then I’m running the code in admin-ajax.php inside the admin folder. I’m not sure why I did that (rather than someplace inside my theme directory) other than that’s what some of posts in this forum said to do.

The Problem is that when wordpress updates it wipes it out. So the question is how can I put this inside my theme? I tried just calling a file inside my theme folder and it didn’t work.

here is my code that I placed inside the existing admin-ajax.php right before the do_action( ‘admin_init’ ); section. Again this works fine inside this file, but it gets wiped out every time I update the version of wordpress.

// ------------------------------
// ------------------------------
//       talent query code
// ------------------------------
// ------------------------------

function get_ajax_posts() {

    // get filters from 3 drop down menus 
    $tax_query = $_POST['filters'];
    
    $location = $tax_query['location'];
    $specialty = $tax_query['specialty'];

    $levels = $tax_query['level'];
    // create levels array for selected level and above
    switch ($levels) {
            case 23:
                $level = array('23'); // Level 1 through 5
                break;
            case 24:
               $level = array('24'); // Level 2 through 5
                break;
            case 25:
                $level = array('25');// Level 3 through 5
                break;
            case 26:
                $level = array('26');// Level 4 and 5
                break;
            case 27:
                $level = '27';// Level 5
                break;
            default:
                $level = array('23', '24', '25', '26', '27');
        }

    // Display Talent if only Level is selected
    if(isset($tax_query['level']) && empty($tax_query['location']) && empty($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'level',
                  'field'    => 'term_id',
                  'terms'    => $level,// 23 (4), 24(4), 25(7), 26(3), 27(3) // array( 25, 26, 27 )
                  
              ),   
           ),
        );
    }
    // Display Talent if only Level and Location is selected
    else if(isset($tax_query['level']) && isset($tax_query['location']) && empty($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'level',
                  'field'    => 'term_id',
                  'terms'    => $level,// 23 (4), 24(4), 25(7), 26(3), 27(3) // array( 25, 26, 27 )
                  
              ), 
              array(
                  'taxonomy' => 'location',
                  'field'    => 'term_id',
                  'terms'    => $location,
              ),  
          ),
        );
    }
    // Display Talent if all three are selected
    else if(isset($tax_query['level']) && empty($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'level',
                  'field'    => 'term_id',
                  'terms'    => $level,// 23 (4), 24(4), 25(7), 26(3), 27(3) // array( 25, 26, 27 )  
              ), 
              array(
                  'taxonomy' => 'specialty',
                  'field'    => 'term_id',
                  'terms'    => $specialty,
              ),  
          ),
        );
    }
    // Display Talent if Level and specialty is selected
    else if(isset($tax_query['level']) && isset($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'level',
                  'field'    => 'term_id',
                  'terms'    => $level,// 23 (4), 24(4), 25(7), 26(3), 27(3) // array( 25, 26, 27 )  
              ), 
              array(
                  'taxonomy' => 'location',
                  'field'    => 'term_id',
                  'terms'    => $location,
              ),
              array(
                  'taxonomy' => 'specialty',
                  'field'    => 'term_id',
                  'terms'    => $specialty,
              ),  
          ),
        );
    }
    // Display Talent if only Location is selected
    else if(empty($tax_query['level']) && isset($tax_query['location']) && empty($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND', 
              array(
                  'taxonomy' => 'location',
                  'field'    => 'term_id',
                  'terms'    => $location,
              ),  
          ),
        );
    }
    // Display Talent if Location and specialty is selected
    else if(empty($tax_query['level']) && isset($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'location',
                  'field'    => 'term_id',
                  'terms'    => $location,
              ),
              array(
                  'taxonomy' => 'specialty',
                  'field'    => 'term_id',
                  'terms'    => $specialty,
              ),  
          ),
        );
    }
    // Display Talent if only specialty is selected
    else if(empty($tax_query['level']) && empty($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'specialty',
                  'field'    => 'term_id',
                  'terms'    => $specialty,
              ),  
          ),
        );
    }
    
    else{
        $args = null;
        //echo "else Args: ". $args;
    }

    wp_reset_query();

    // The Query
    $ajaxposts = new WP_Query( $args );

    $response = '';

    // The Query
    if ( $ajaxposts->have_posts() ) {
        while ( $ajaxposts->have_posts() ) {
            $ajaxposts->the_post();
            //$response .= get_template_part('products');

            $response .= "";

            $name = get_field('name');
            $main_image = get_field('main_image');

         ?>

        <div class="col-sm-6 col-md-3 talent-col">
          <div class="talent">
            <a type="button" href="<?php the_permalink() ?>">
             <img class="img-responsive" src="<?php echo $main_image; ?>">
             <h3 class="dark"><?php echo $name; ?></h3> 
            </a>
          </div><!-- close talent -->
        </div><!-- close col -->

       <?php
     
       }// end while
    } else {
        $response .= get_template_part('none');
    }
    
    exit; // leave ajax call
}// end get_ajax_posts

// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_get_ajax_posts', 'get_ajax_posts');
add_action('wp_ajax_nopriv_get_ajax_posts', 'get_ajax_posts');


//===================
// end talent query code
//===================
0
Aaron4osu 3 weeks 0 Answers 5 views 0

Leave an answer