【问题标题】:Ajax filter for wordpresswordpress 的 Ajax 过滤器
【发布时间】:2021-03-28 13:14:20
【问题描述】:

经过大量研究,我发现了这个:https://rudrastyh.com/wordpress/ajax-post-filters.html

但我只有在选择了两个选项时才能让它工作。

我会给你一些背景信息: 我有一个名为“Contratistas”的 CPT,它有两个自定义分类法“especialidad”和“industria”,它们都有两个术语,每个“especialidad”->“tecnologia”和“auditoria”; 'industria' -> 'cultivo' 和 'depocito'

这是我的功能:

function misha_filter_function(){
    $args = array(
        'orderby' => 'date', // we will sort posts by date
        'order' => $_POST['date'], // ASC or DESC
        'post_per_page' => -1,
        'post_type' => 'contratista'
    );
    // for taxonomies / categories
    if( isset( $_POST['filtroEspecialidad'] ) && isset ($_POST['filtroIndustria'])  ) {
        $args['tax_query'][] = array(
         //'relation' => 'AND',
            array(
                'taxonomy' => 'especialidad',
                'field' => 'id',
                'terms' => $_POST['filtroEspecialidad']
            ),
            array(
                'taxonomy' => 'industria',
                'field' => 'id',
                'terms' => $_POST['filtroIndustria']
            ),
        );
    
    } elseif( !isset($_POST['filtroEspecialidad'] ) && isset($_POST['filtroIndustria'])  ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'industria',
            'field' => 'id',
            'terms' => $_POST['filtroIndustria']
        );
    
    } elseif( isset( $_POST['filtroEspecialidad'] ) && !isset($_POST['filtroIndustria'])  ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'especialidad',
            'field' => 'id',
            'terms' => $_POST['filtroEspecialidad']
        );
    }

如果你从两个分类中选择一些东西,它会起作用,但是当一个为空时,它会说“没有帖子”

作为奖励,我想在过滤之前显示所有帖子。

我希望有人可以帮助我!谢谢!我对 Wordpress 还很陌生

编辑!这是我的 js 和我的表单,我在这里不知所措我不知道出了什么问题:(

jQuery(function($){
    $('#filter').submit(function(){
        var filter = $('#filter');
        $.ajax({
            url:filter.attr('action'),
            data:filter.serialize(), // form data
            type:filter.attr('method'), // POST
            beforeSend:function(xhr){
                filter.find('.filtrar').text('Procesando...'); // changing the button label
            },
            success:function(data){
                filter.find('.filtrar').text('Filtrar'); // changing the button label back
                $('#response').html(data); // insert data
            }
        });
        return false;
    });

我的 php 文件:

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
                    <div class="titulo mb-3">
                        <h3>Especialidad</h3>
                    </div>
                    <?php
                        if( $terms = get_terms( array( 'taxonomy' => 'especialidad', 'orderby' => 'name' ) ) ) : 
                 
                            echo '<select name="filtroEspecialidad"><option value="">Seleccione una especialidad...</option>';
                            foreach ( $terms as $term ) :
                                echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
                            endforeach;
                            echo '</select>';
                        endif;
                        
                    ?>
                    <div class="titulo my-3">
                        <h3>Industrias</h3>
                    </div>
                    <?php   
                        if( $terms = get_terms( array( 'taxonomy' => 'industria', 'orderby' => 'name' ) ) ) : 
                 
                            echo '<select name="filtroIndustria"><option value="">Seleccione una industria...</option>';
                            foreach ( $terms as $term ) :
                                echo '<option  value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
                            endforeach;
                            echo '</select>';
                        endif;
                    ?>  
                    <button class="my-3 filtrar">Filtrar</button>
                    <a href="" id="clear">Clear</a>
                    <input type="hidden" name="action" value="myfilter">
                </form>

【问题讨论】:

    标签: wordpress filter taxonomy


    【解决方案1】:

    您可能想再看一眼Taxonomy Parameters of WP_Query()。不幸的是,WordPress 在上下文中使用 id 参数名称有点松散。我也不完全确定您最初的“两者”是否像您想要的那样工作,因为'field' =&gt; 'id' 实际上是无效的。

    来自文档:

    field (string) – 选择分类术语。可能的值为term_idnameslugterm_taxonomy_id。默认值为term_id

    id 实际上不是一个有效的选项。如果你只是使用term_id,你应该可以省略它。您还可以根据是否设置了该过滤器以编程方式添加 tax_query 数组参数,而不是检查“both setthis set/that unsetthis unset, that set”,也许像这样?

    function misha_filter_function(){
        $args = array(
            'orderby'       => 'date', // we will sort posts by date
            'order'         => $_POST['date'], // ASC or DESC
            'post_per_page' => -1,
            'post_type'     => 'contratista'
        );
    
        if( isset($_POST['filtroEspecialidad']) || isset($_POST['filtroIndustria']) ){
            $args['tax_query'] = array();
            
            if( isset($_POST['filtroEspecialidad']) ){
                $args['tax_query'][] = array(
                    'taxonomy' => 'especialidad',
                    'terms'    => $_POST['filtroEspecialidad']
                );
            }
            
            if( isset($_POST['filtroIndustria']) ){
                $args['tax_query'][] = array(
                    'taxonomy' => 'industria',
                    'terms'    => $_POST['filtroIndustria']
                );
            }
    
            if( count($args['tax_query']) > 1 ){
                $args['tax_query']['relation'] = 'AND';
            }
        }
    
        // Run WP_Query with new $args, etc.
    }
    

    我不确定 $_POST 值是数组还是单个数字,但如果您使用用户提供的输入,您可能希望使用 array_map 和/或 absint 验证这些值,但如果它们'只是 ID,上面的方法现在应该可以了。

    【讨论】:

    • 这很有意义,并且可以更好地使用代码,但它仍然只在选择“especialidad”和“industria”时才有效,但我认为这是方式,所以我会用它!谢谢!! (你知道如何在过滤之前显示所有帖子吗?)
    • 不幸的是,如果上述方法不起作用(它应该),可能还有其他问题在起作用,特别是取决于您调用函数的方式/时间,无论是修改/替换主查询还是不是,$_POST['filtro...'] 变量是否作为空白字符串发送,等等(检查以确保如果没有选择它们就不会被发送)。回复:显示之前,这又取决于原始页面查询在您的特定网站上的样子
    • 我已经更新了我的代码!谢谢你的帮助!!
    • .serialize() 函数传递空值。您可以尝试用data: $('#filter :input').filter(function(index, element) { return $(element).val() != ''; }).serialize(), 替换data:filter.serialize(), 以去除空值和/或使用empty($_POST['filter_name_here']) 而不是isset()
    • 谢谢!!!!有用!!!你给了我很多学习的话题! (我尝试了“空”的方法,现在我将检查另一种!)
    猜你喜欢
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2018-02-25
    相关资源
    最近更新 更多