首页 > 解决方案 > 如何显示在 php 中选择的特定类别的帖子?

问题描述

我正在尝试在 wordpress 中的现有插件上添加巴士公司过滤器。我已经能够从许多列表中选择一家巴士公司,但是当我按 Enter 时,会显示所有公司提供的所有巴士。我只想看到从campany中选择的公共汽车。任何帮助将不胜感激。

这是我选择巴士公司的功能

function wbtm_bus_name($name, $value = ''){
global $post;
if($post){
$values     = get_post_custom( $post->ID );
}else{
$values ='';
}
$company_name  = 'wbtm_bus_company';

$taxonomies = get_terms($company_name, array(
  "orderby"    => "count",
  'hide_empty' => false,
) );

$hierarchy = _get_term_hierarchy($company_name);


if ( ! empty( $taxonomies ) && ! is_wp_error( $taxonomies ) ) : ob_start(); ?>

    <select name="<?php echo $name; ?>" class='seat_type select2'>

        <option value=""><?php _e('Select a company','bus-ticket-booking-with-seat-reservation')?></option>

        <?php
        // *************custom for form campany ****************
        foreach ( $taxonomies as $taxonomie ) : 

            $selected = $type_name == $taxonomie->name ? 'selected' : '';

            if( ! empty( $value ) ) $selected = $taxonomie->name == $value ? 'selected' : '';
            printf( '<option %s value="%s">%s</option>', $selected, $taxonomie->name, $taxonomie->name );

        endforeach; ?>

    </select>

<?php endif;
          $content = ob_get_clean();
          return $content;
}

这是我的巴士搜索功能:

function wbtm_bus_search($atts, $content=null, $name){
$company = isset($_GET['bus_company_detail']) ? strip_tags($_GET['bus_company_detail']) : '';

 <div class="wbtm-search-result-list">

        <?php
        $paged = get_query_var("paged") ? get_query_var("paged") : 1;
        $meta_query_args = array(
            array(
                'key' => 'wbtm_bus_bp_stops',
                'value' => $start,
                'compare' => 'LIKE',
            ),

            array(
                'key' => 'wbtm_bus_next_stops',
                'value' => $end,
                'compare' => 'LIKE',
            ),

        );


        if (!empty($company)) {
            $arg3 = array(
                'key' => 'bus_company_detail',
                'terms' => $company,
                'compare' => 'IN',
                'field' => 'term_id',
                'value' => 'bus_company_detail',

            );
            array_push($meta_query_args,$arg3);
        } 
        // print_r(meta_query_args);
        $args_search = array(
            'post_type' => array('wbtm_bus'),
            'paged' => $paged,
            'posts_per_page' => $show,
            'meta_query' => array(
                'relation' => 'AND',
                $meta_query_args
            )    
        );
        if(!empty($bus_cat))   {
            $arg4 = array(
                'post_type' => array('wbtm_bus'),
                'tax_query' => array(
                    array(
                        'taxonomy' => 'bus_company_detail',
                        'field' => 'term_id',
                        'terms' => $bus_cat,
                    ),
                ),

            );
            $args_search = array_merge($args_search, $arg4);
        }
     }

标签: phpwordpressfiltering

解决方案


推荐阅读