带过滤器的搜索查询

时间:2020-12-23 19:25:21

标签: javascript wordpress search filter

多亏了堆栈溢出的帮助,我才能使我的过滤器工作(Ajax filter for wordpress),但现在我想添加一个搜索输入来搜索我的帖子标题(一种名为“Contratistas”的自定义帖子类型) )。 所以我有几个问题:

  1. 我的过滤器表单有 POST,但搜索(我看过示例)总是有 GET,所以,我应该有两个单独的表单吗?如果是这样,有没有办法在没有按钮的情况下提交我的搜索?这是我的代码:
<form id="searchform" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="text" class="search-field mb-3" name="s" placeholder="Search" value="<?php echo get_search_query(); ?>">
    <input type="submit" value="Search" class="mb-3">
</form>
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
    <div class="titulo mb-3">
        <h3>Región</h3>
    </div>
    <?php
            if( $terms = get_terms( array( 'taxonomy' => 'region', 'orderby' => 'name' ) ) ) : 
     
                echo '<select name="filtroRegion"><option value="">Seleccione una región</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>
            <button><a href="" id="clear">Limpiar filtros</a></button>
            <input type="hidden" name="action" value="myfilter">
</form>

如你所见,我有两个按钮,但我只想要最后一个('Filtrar')

  1. 我不知道如何使用过滤器实现搜索,以便我可以将结果与从下拉列表中得到的结果相同。

这是我的过滤器:

$args = array(
    'orderby' => 'date', // we will sort posts by date
    'order' => $_POST['date'], // ASC or DESC
    'post_per_page' => -1,
    'post_type' => 'contratista'
);

if (!empty($_POST['filtroRegion']) || !empty($_POST['filtroIndustria'])) {
    $args['tax_query'] = array();

    if (!empty($_POST['filtroRegion'])) {
        $args['tax_query'][] = array(
            'taxonomy' => 'region',
            'terms' => $_POST['filtroRegion']
        );
    }

    if (!empty($_POST['filtroIndustria'])) {
        $args['tax_query'][] = array(
            'taxonomy' => 'industria',
            'terms' => $_POST['filtroIndustria']
        );
    }

    if (count($args['tax_query']) > 1) {
        $args['tax_query']['relation'] = 'AND';
    }
}

$query = new WP_Query($args);

还有我的 JQuery:

jQuery(function($) {
    $('#filter').submit(function() {
        var filter = $('#filter');
        $.ajax({
            url: filter.attr('action'),
            data: $('#filter :input').filter(function(index, element) { return $(element).val() != ''; }).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;
    }
}

任何帮助或指导将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

我明白了!这是我的工作代码,以防其他人需要它! 答案很简单!我只需要添加这个:

 's'             => $_POST['s']

这是我的功能

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',
        's'             => $_POST['s'],
    );

//Same code as before:
    
    if( !empty($_POST['filtroRegion']) || !empty($_POST['filtroIndustria']) ){
        $args['tax_query'] = array();
        
        if(!empty($_POST['filtroRegion']) ){
            $args['tax_query'][] = array(
                'taxonomy' => 'region',
                'terms'    => $_POST['filtroRegion']
            );
        }


    }

    $query = new WP_Query( $args );
    
    //my post 

还有我的表格:

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
    <h3>Buscar</h3>
    <input type="text" class="search-field mb-3" name="s" placeholder="Busqueda por nombre" value="<?php echo get_search_query(); ?>">
    <div class="titulo mb-3">
        <h3>Región</h3>
    </div>
    <?php
        if( $terms = get_terms( array( 'taxonomy' => 'region', 'orderby' => 'name' ) ) ) : 
            echo '<select name="filtroRegion"><option value="">Seleccione una región</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;
                            
    ?>

相关问题