繁体   English   中英

如何获取 Wordpress 后置过滤器(misha_filter_function)来过滤标签关联的 CPT 类别

[英]How to get Wordpress post filter (misha_filter_function) to filter CPT categories associated by a tag

为清晰而编辑:

我正在使用来自: https://rudrastyh.com/wordpress/ajax-post-filters.html的 misha_filter_function 来过滤帖子。

初始数组通过 location_and_season(使用 ACF 制作)标签显示正确的帖子。 但是第二个数组发生了一些奇怪的事情,因为当我单击下拉列表 select 以按类别对帖子进行排序时,它会过滤帖子类型中的所有帖子,并有效地忽略“标签”=> $ 值,

我对 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 function 的东西。 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
    }
});

另外,您是否使用wp_localize_script将 js 脚本排入队列?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM