AJAX过滤器-显示至少包含一个过滤器的所有结果

时间:2020-07-02 作者:Jandon

对于一个项目,我使用WordPress的木材(细枝)。

我正在尝试用视频模板上的标签实现ajax过滤器。但它不像我想要的那样工作。目前我的制度太严格了。过滤器以错误的方式组合。

示例:

  • Video 1
    标签:media // digital // health

  • Video 2
    标签:media // clinical // private sector

    如果我选择mediadigital 在我的过滤器里Video 1 因此,因为这是唯一一个获得这两个标签的视频Video 2 收到media

    我想显示至少包含一个过滤器的所有视频。我想累积Video 1Video 2 结果是因为Video 2 至少包含一个筛选器(media).

    能帮我个忙吗?

    这是我的代码:

    tpl\\U视频。php

    $context[\'get_page\'] = empty($_GET[\'get_page\']) ? 1 : $_GET[\'get_page\'];
    $context[\'cards_per_page\'] = empty($_GET[\'cards_per_page\']) ? 10 : $_GET[\'cards_per_page\'];
    
    $context[\'videos\'] = Timber::get_posts(array(
        \'post_type\' => \'videos\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => $context[\'cards_per_page\'],
        \'paged\' => $context[\'get_page\'],
        \'orderby\' => \'menu_order\',
        \'order\' => \'ASC\'
    ));
    
    $context[\'nb_videos\'] = Timber::get_posts(array(
        \'post_type\' => \'videos\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1
    ));
    
    $context[\'thematiques_list\'] = get_terms( array(
        \'taxonomy\' => \'thematique\',
        \'hide_empty\' => true
    ));
    
    脚本。js公司
    function filters_video() {
        if ($(\'.page-videos\').size() > 0) {
            if($(\'.page-videos .aside .aside__list\').size() > 0) {
                $(\'.page-videos .aside .module-tags__item\').click(function(e) {
                    e.preventDefault();
                    $(this).toggleClass(\'active\');
                    $(\'.module-pagination .module-pagination__link.active\').removeClass(\'active\');
                    $(\'.module-pagination .module-pagination__link\').eq(0).addClass(\'active\');
                    load_videos();
                });
            }
    
            if($(\'.page-videos .module-pan__list .aside__list-list\').size() > 0) {
                $(document).on(\'click\', \'.page-videos .module-pan__list .aside__list-item\', function(e) {
                    e.preventDefault();
                });
            }
    
            if($(\'.module-pagination\').size() > 0) {
                $(document).on(\'click\', \'.module-pagination .module-pagination__link\', function(e) {
                    e.preventDefault();
                    if(!$(this).hasClass(\'inactive\')) {
                        $(\'.module-pagination .module-pagination__link.active\').removeClass(\'active\');
                        $(this).addClass(\'active\');
    
                        load_videos();
                    }
                });
            }
        }
    }
    
    function load_videos() {
        var page = parseInt($(\'.module-pagination .module-pagination__link.active\').eq(0).text());
        var cats = [];
        $(\'.page-videos .module-tags .module-tags__item.active\').each(function(i) {
            cats.push($(this).attr(\'data-term-id\'));
        });
    
        $(\'.list__ajax\').html("");
        $(\'.page-videos .module-pan__list\').prev(\'.loader__wrapper\').find(\'.loader\').clone().appendTo(\'.list__ajax\');
    
        $.ajax({
            type: \'POST\',
            url: \'/wp-admin/admin-ajax.php\',
            dataType: \'html\',
            data: {
                \'action\' : \'load_videos\',
                \'cats\' : cats.join(\',\'),
                \'get_page\' : page
            },
            success: function(data) {
                $(\'.list__ajax\').html(data);
            },
            error: function(data) {
                console.log(data);
            }
        });
    }
    

    功能。php

    add_action( \'wp_ajax_nopriv_load_videos\', \'load_videos\' );
    add_action( \'wp_ajax_load_videos\', \'load_videos\' );
    function load_videos() {
        $context = Timber::get_context();
    
        $cats = explode(\',\', empty($_POST[\'cats\']) ? array() : $_POST[\'cats\']);
        $context[\'cards_per_page\'] = 6;
        $context[\'get_page\'] = empty($_POST[\'get_page\']) ? 1 : $_POST[\'get_page\'];
    
        $tax_query = array();
        foreach($cats as $id) {
            array_push($tax_query, array(
                \'taxonomy\' => \'thematique\',
                \'field\' => \'term_id\',
                \'terms\' => $id
            ));
        }
    
        $context[\'videos\'] = Timber::get_posts(array(
            \'post_type\' => \'videos\',
            \'post_status\' => \'publish\',
            \'posts_per_page\' => $context[\'cards_per_page\'],
            \'paged\' => $context[\'get_page\'],
            \'tax_query\' => $tax_query
        ));
    
        $context[\'nb_videos\'] = Timber::get_posts(array(
            \'post_type\' => \'videos\',
            \'post_status\' => \'publish\',
            \'posts_per_page\' => -1,
            \'tax_query\' => $tax_query
        ));
    
        Timber::render( \'bloc_video.twig\', $context );
    
        die();
    }
    
    tpl\\U视频。细枝
    <div class="video-list__list">
        <div class="list__ajax">
            {% include "bloc_video.twig" with {\'posts\': posts, \'nb_videos\': nb_videos, \'cards_per_page\': cards_per_page, \'get_page\': get_page} %}
        </div>
    </div>
    

1 个回复
最合适的回答,由SO网友:Ivan Shatsky 整理而成

尝试替换

    $tax_query = array();
使用

    $tax_query = array( \'relation\' => \'OR\' );
在您的functions.php 文件