Как передать слаг терма таксономии (taxonomy term slug) в файл functions.php в wordpress
Мне нужен код, который будет подгружать посты по три штуки в div через WP ajax на странице CPT терминов таксономии (taxonomy term template). Мой код загружает сообщения, если я указываю только CPT без термина таксономии, но мне нужно, чтобы он загружал сообщения только с термином таксономии на странице шаблона таксономии. Моя проблема в том, что я не могу передать слаг термина в код в файле functions.php, он не видит его там 'terms' => $term_slug,. Я слышал, что это можно сделать с помощью функции WP_Term_query, но я не мог понять, как это сделать. Буду признателен за любое рабочее решение. Спасибо! Мой код ниже.
functions.php
add_action( 'wp_ajax_load_tax_results', 'load_tax_results' );
add_action( 'wp_ajax_nopriv_load_tax_results', 'load_tax_results' );
function load_tax_results() {
$termid = get_queried_object()->term_id;
$myterm = get_term($termid, 'product-type');
$term_slug = $myterm->slug;
$args = array(
'post_type' => 'site-product',
'posts_per_page' => 3,
'paged' => $_POST['paged'],
'tax_query' => array(
array(
'taxonomy' => 'product-type',
'field' => 'slug',
'terms' => $term_slug,
),
),
);
$wquery = new WP_Query($args);
if( $wquery->have_posts() ) :
while($wquery->have_posts() ) : $wquery->the_post();
?>
// stuff
<?php
endwhile;
wp_reset_postdata();
endif;
wp_die();
}
custom.js
jQuery(document).ready(function($){
let loadMoreButton;
loadMoreButton = $('#comp-load-more-btn');
let currentPage;
currentPage = 1;
loadMoreButton.on('click', function(e) {
currentPage++;
e.preventDefault();
var button = $(this);
$.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'post',
data: {
'action': 'load_tax_results',
'paged': currentPage,
},
beforeSend: function() {
button.text('Загрузка...');
},
success: function(response) {
// Append the new posts to the container
$('#posts-here').append(response);
button.text('Загрузить ещё...');
// Hide the button if there are no more pages
if ('' === response) {
button.hide();
}
}
});
});
});