如何获取Wordpress post-filter(misha_filter_function)来过滤与标签关联的CPT



为清晰度编辑:

我使用的misha_filter_function来自:https://rudrastyh.com/wordpress/ajax-post-filters.html以筛选帖子。

初始阵列通过location_and_treason(使用ACF制作(标签显示正确的帖子。但第二个数组发生了一些奇怪的事情,因为当我点击下拉选择按类别对帖子进行排序时,它会过滤帖子类型中的所有帖子,并有效地忽略"tag"=>$value,

我对Wordpress PHP和JS还比较陌生,所以我的Google Fu并没有太大帮助,因为我真的不知道该搜索什么。如有任何帮助,我们将不胜感激。谢谢

这是过滤器代码:

<?php
add_action('wp_ajax_myfilter', 'misha_filter_function'); // 
wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');
function misha_filter_function()
{
$value = get_field('location_and_season');
$args = array(
'post_type' => 'shows',
'tag' => $value,
'posts_per_page' => - 1, // show all posts.
'orderby' => 'name', // we will sort posts by name
'order' => 'ASC'
//$_POST['name'] // ASC or DESC
);
// for taxonomies / categories
// IMPORTANT! Adding && !empty( $_POST['categoryfilter'] ) fixes the no posts found for All Categories
if (isset($_POST['categoryfilter']) && 
!empty($_POST['categoryfilter'])) $args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['categoryfilter']
)
);
$query = new WP_Query($args);
// The Query
query_posts($args);
// The Loop
while (have_posts()):
the_post(); ?>

这是前端下拉代码:

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" 
method="POST" id="filter" style="float:right; margin-right: 15px;">
<?php
if ($terms = get_terms(array(
'taxonomy' => 'category',
'orderby' => 'name'
))):
echo '<select name="categoryfilter"><option id="refresh" value="all" 
class="dropdown-select">All Topics...</option>';
foreach ($terms as $term):
echo '<option value="' . $term->term_id . '">' . $term->name . 
'</option>'; 
endforeach;
echo '</select>';
endif;
?>
<div class="processing" style="height:30px;"></div>
<input type="hidden" name="action" value="myfilter">
</form>

和JS

jQuery(function ($) {
$('#filter').change(function () {
var filter = $('#filter');
$.ajax({
url: filter.attr('action'),
data: filter.serialize(), // form data
type: filter.attr('method'), // POST
beforeSend: function (xhr) {
filter.find('.processing').html('<img src="spinner.gif" class="spinner">'); // changing the button label
},
success: function (data) {
filter.find('.processing').text(''); // changing the button label back
$('#response').html(data); // insert data
}
});
$(document).ready(function () {
$('#filter').on("click", function () {
$('.post-section').toggleClass("active");
});
});

return false;
});

我没有看到任何实际调用php函数的东西。Wordpress ajax调用需要在数据中执行操作。

$.ajax({
url: filter.attr('action'),
type: filter.attr('method'), // POST
data: {
action: 'misha_filter_function',
categoryfilter:  $(select['name ="categoryfilter"]').val()
},
beforeSend: function (xhr) {
filter.find('.processing').html('<img src="spinner.gif" class="spinner">'); // changing the button label
},
success: function (data) {
filter.find('.processing').text(''); // changing the button label back
$('#response').html(data); // insert data
}, 
failure: function(response){
console.log(response); //Debug only
}
});

另外,您是否将js脚本与wp_localize_script一起入队?

最新更新