wp_query自定义帖子类型

时间:2016-09-15 22:10:20

标签: wordpress custom-post-type custom-taxonomy

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

<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;

&GT;

使用上述查询

,自定义搜索模板为search-wego_business.php

1 个答案:

答案 0 :(得分:0)

也许您错过了post_type字段,您也可以在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
);

否则,我需要更多详细信息 - 例如:您的分类和自定义帖子类型声明以及归档的完整来源(至少是构建搜索查询的部分)。

相关问题