2016-09-15 106 views
0

我不知道我是否看不到发束,但我需要一些帮助。我有一个自定义职位类型“业务”与2个相关的自定义分类,business_category和business_tags。在自定义存档模板上,我有一个自定义搜索表单:wp_query自定义帖子类型

<form class="search-form" role="search" method="get" action="<?php echo home_url('/'); ?>"> 
     <div class="form-group"> 
      <label for="search-input"><i class="fa fa-search" aria-hidden="true"></i><span class="screen-reader-text">Search icons</span></label> 
      <input type="hidden" name="post_type" value="wego_business" /> 
      <input type="search" class="form-control search-field" placeholder="Search the Directory" value="" name="s" id="s"> 
     </div> 
    </form> 

然后显示在自定义搜索模板上。我无法获得business_tags的结果

global $wp_query; 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$tag = get_search_query(); 
$args = array (
    's'      => $s, 
    'order'     => 'ASC', 
    'orderby'    => 'name', 
    'tax_query' => array(
     array(
      'taxonomy' => 'business_tags', 
      'field' => 'slug', 
      'terms' => $tag, 
     ), 
    ), 
    'paged'     => $paged 
); 

AM我缺少明显的东西?

这里是CPT创作:

$labels = array(
    'name'    => _x('Business', 'Post Type General Name', 'wego_fran'), 
    'singular_name'  => _x('Business', 'Post Type Singular Name', 'wego_fran'), 
    'menu_name'   => __('Business', 'wego_fran'), 
    'parent_item_colon' => __('Parent Business:', 'wego_fran'), 
    'all_items'   => __('All Business', 'wego_fran'), 
    'view_item'   => __('View Business', 'wego_fran'), 
    'add_new_item'  => __('Add New Business', 'wego_fran'), 
    'add_new'    => __('Add New', 'wego_fran'), 
    'edit_item'   => __('Edit Business', 'wego_fran'), 
    'update_item'   => __('Update Business', 'wego_fran'), 
    'search_items'  => __('Search Business', 'wego_fran'), 
    'not_found'   => __('Not found', 'wego_fran'), 
    'not_found_in_trash' => __('Not found in Trash', 'wego_fran'), 
); 
$args = array(
    'label'    => __('Business', 'wego_fran'), 
    'description'   => __('Business', 'wego_fran'), 
    'labels'    => $labels, 
    'supports'   => array('title', 'editor', 'thumbnail', 'page-attributes'), 
    'taxonomies'   => array('business_category', 'business_tags'), 
    'hierarchical'  => true, 
    'public'    => true, 
    'show_ui'    => true, 
    'show_in_menu'  => true, 
    'show_in_nav_menus' => true, 
    'show_in_admin_bar' => true, 
    'menu_position'  => 20, 
    'menu_icon'   => 'dashicons-admin-home', 
    'can_export'   => true, 
    'has_archive'   => true, 
    'exclude_from_search' => false, 
    'publicly_queryable' => true, 
    'capability_type'  => 'post', 
    'rewrite'    => array('slug' => 'business'), 
); 
register_post_type('wego_business', $args); 

<?php 
global $wp_query; 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$tag = get_search_query(); 
$args = array (
    'post_type'    => 'wego_business', 
    's'      => $s, 
    'order'     => 'ASC', 
    'orderby'    => 'name', 
    'tax_query' => array(
     array(
      'taxonomy' => 'business_tags', 
      'field' => 'slug', 
      'terms' => $tag, 
     ), 
    ), 
    'paged'     => $paged 
); 

// The Query 
$wp_query = new WP_Query($args); 

$count = $wp_query->found_posts; 

>

自定义搜索模板是使用上面的查询

+0

首先:您的自定义搜索模板的名称是什么?你确定*它正在加载该模板? (您的表单将触发内置/默认的“WordPress搜索”)。其次,您的纳税查询似乎在寻找“slu”“ - 您是否在搜索输入中输入”slug“?第三 - 正如下面的答案指出的那样 - 你没有'post_type'参数,所以你的参数默认只会查询'posts'。最后,你没有包含运行你的查询的代码,或者输出结果的代码,所以我们只能假设你确信查询执行正确,等等。 –

回答

0

也许你错过了post_type场搜索wego_business.php和您还可以在name字段内搜索。一个重构会是什么样子:

$args = array (
    'post_type' => 'business', 
    's'      => $s, 
    'order'     => 'ASC', 
    'orderby'    => 'name', 
    'tax_query' => array(
     array(
      'taxonomy' => 'business_tags', 
      'field' => 'name', 
      'terms' => $tag, 
     ), 
    ), 
    'paged' => $paged 
); 

否则,我会需要更多的细节 - 这样的:你的分类和自定义后类型声明和归档的完整源(至少是构建搜索查询的部分)。

+0

从OP的代码中,你可以看到帖子类型应该是'flights_business',而不是'business' –

+0

是的@cale_b - 在描述OP中说'业务',表单名称为attr。这是'flights_business' - 也许完整的代码将帮助我们更好地检查它! :) –

相关问题