You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:实现自定义文章类型搜索框与现有过滤器协同工作

问题:自定义文章类型过滤器与搜索框协同失效

我在自定义文章类型(Custom Post Type)上已实现基于分类法、年份和语言的内容过滤器,希望添加搜索框使其与现有过滤器协同工作。但自行编写的代码仅搜索功能可用,其他过滤器全部失效,相关代码如下:

原过滤器函数代码

function get_bp_results() {

    $company = clean_field('bp-company');
    $year = clean_field('bp-year');
    $languages = clean_field('bp-languages');
    $keywords = get_keywords_array();

    $tax_query = $date_query = $post_types = [];

    $all_langs = !empty($languages) && $languages == '1';

    if (!empty($year)) {
        $date_query[] = [
            'year' => $year,
        ];
    }

    if (!empty($company)) {
        $tax_query[] = [
            'taxonomy'  => 'company',
            'field'     => 'term_id',
            'terms'     => $company
        ];
    }

    if (!empty($keywords)) {
        foreach ($keywords as $kw) {
            $tax_query[] = [
                'taxonomy'  => 'keywords',
                'field'     => 'term_id',
                'terms'     => $kw
            ];
        }
    }

    if ($all_langs) {
        foreach (get_option('lexoseo__langs') as $key => $value) {
            $post_types[] = "cpt-bestpractices-{$key}";
        }
    } else {
        $post_types[] = "cpt-bestpractices-{$_SESSION['jez']}";
    }

    $args = [
        'numberposts'       => -1,
        'orderby'           => 'menu_order',
        'order'             => 'ASC',
        'post_status'       => 'publish',
        'post_type'         => $post_types,
        'suppress_filters'  => true,
        'tax_query'         => $tax_query,
        'date_query'        => $date_query,
    ];

    $results = new WP_Query($args);

    ob_start();

    if ($results->have_posts()) { ?>
        <div id="bp-results">
            <?php while ($results->have_posts()) {
                
                $results->the_post();

                $bp_post = get_post();

                get_template_part('template-parts/bp-block', null, [
                    'bp_post' => $bp_post,
                    'new_tab' => true
                ]);
            } ?>

    </div>     
        </div>

        <?php wp_reset_postdata(); ?>
    <?php } else { ?>
        <div id="no-bp-results">
            <h3><?php echo __lt('frontend', 'no_bp_results', $_SESSION['jez']); ?></h3>
        </div>
    <?php }

    if (wp_doing_ajax()) {
        wp_send_json_success([
            'message' => handle_manual_hyphenation(ob_get_clean())
        ]);
    } else {
        return handle_manual_hyphenation(ob_get_clean());
    }
}

尝试编写的搜索相关函数代码

function fetch_ajax() {
?>
<script type="text/javascript">
function fetch(){
    jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'data_fetch', query: jQuery('#search').val() },
        success: function(data) {
            jQuery('#bp-results-inner').html( data );
        }
    });
}
</script>
<?php
}
add_action( 'wp_footer', 'fetch_ajax' );

function data_fetch(){
    $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => $_POST['query'], 'post_type' => array('cpt-bestpractices-en','cpt-bestpractices-de') ) );
    if( $the_query->have_posts() ):?>
    <div id="bp-results">

      <?php while( $the_query->have_posts() ): $the_query->the_post(); 

      ?>
           
            <?php get_template_part('template-parts/bp-block')?>
            
       
        <?php endwhile; ?>
        </div>
       <?php wp_reset_postdata();  
    endif;
    die();
}

内容的提问来源于stack exchange,提问作者Aleksandar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:07:06