WordPress ajax

  wordpress

functions.php

function getBedroomsCount() {
    $query = $_POST['pageId'];
    $args = array(
        'post_type' => 'page',
        'post_status' => 'publish',
        'p' => $query,
    );
    $search = new WP_Query( $args );
    
    ob_start();
    
    if ( $search->have_posts() ):
        while ( $search->have_posts() ) : $search->the_post();
            include(locate_template('template-parts/content-product.php'));
        endwhile;
    else:
        echo '<p>brak strony</p>';
    endif;
    
    $content = ob_get_clean();
    echo $content;
    die();
}

plik.js

function getBedroomsCount(city) {
          let ajaxUrl = php_vars.ajax_url,
              content = $('[name="bedrooms"]');

          jQuery.ajax({
            type : 'post',
            url : ajax_url,
            data : {
                action : 'getBedroomSelect',
                city : city
          },
          beforeSend: function() {
            content.addClass('loading');
          },
          error: function( error ) {
            console.log(error);
          },
          success : function( response ) {
            console.log(response);
            // content.removeClass('loading');
            // content.html( response );
          }
        })
}
add_action('wp_ajax_getBedroomSelect', 'getBedroomSelect');
add_action('wp_ajax_nopriv_getBedroomSelect', 'getBedroomSelect');

ajax url

domena.pl/wp-admin/admin-ajax.php,