Chuyển mảng javascript sang php ajax

Tôi có một hàm ajax kết nối với tệp php. Tôi đang cố gắng chuyển các đối số [‘tax_query’] được thu thập trên trang tới một truy vấn chạy trên php. Làm cách nào tôi có thể tạo một mảng [hoặc có thể chỉ là một chuỗi] trong javascript và chuyển nó làm đối số cho hàm bên trong php. Mọi thứ đều hoạt động ngoại trừ việc tôi có các đối số truy vấn trong tệp php được mã hóa cứng nơi tôi muốn chúng động dựa trên những gì tôi chuyển qua yêu cầu ajax

Đây là hàm jQuery của tôi lấy dữ liệu từ các lựa chọn của người dùng, lưu các biến và kết nối với php thông qua ajax

[function[$] {

  function getFilters [] {
    var filters = {}
    
    $['.checked'].each[function [] {
      filters[this.dataset.filter] = this.dataset.value
    }]
    
      return filters
    }

    $[".dropdown-menu li a"].click[function [event] {
      var $this = $[this]
      var $dropdown = $this.parents['.dropdown']
      var html = $this.text[] + ' '
      
      $dropdown.find[".btn"].html[html];  
      $dropdown.find['a'].removeClass['checked'];
      $this.addClass['checked'];

      var filters = getFilters[];

      //var level = filters["level"];
      var level = filters.level;
      var location = filters.location;
      var specialty = filters.specialty;
      
      alert["Level: "+level+" Location: "+location+" Specialty: "+specialty];

      console.log[getFilters[]];

      event.preventDefault[];

      alert["made it up to ajax"];
   
      $.ajax[{
          type: 'POST',
          url: '',
          dataType: "html", // add data type
          data: { action : 'get_ajax_posts' },
          success: function[ response ] {
              console.log[ response ];

              $[ '.posts-area' ].html[ response ]; 
          }
      }];      
    }]

}][jQuery];

đây là hàm php của tôi chạy truy vấn bài đăng Wordpress và quay lại jquery

function get_ajax_posts[] {
    // Query Arguments
    $args = array[
      'post_type' => 'the-talent',
      'tax_query' => array[
          'relation' => 'AND',
          array[
              'taxonomy' => 'level',
              'field'    => 'term_id',
              'terms'    => array[ '25' ],// needs to be 3 and up? maybe use if statements?
          ],
          array[
              'taxonomy' => 'location',
              'field'    => 'term_id',
              'terms'    => array[ '20' ],
          ],
          array[
              'taxonomy' => 'specialty',
              'field'    => 'term_id',
              'terms'    => array[ '4' ],
          ],
          
      ],
    ];

    // 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'];

         ?>

        

Chủ Đề