仅显示特定类别的自定义帖子类型

时间:2017-09-17 作者:JohnSnow

我有一些faqs 在我的网站上,只想显示特定类别下的常见问题。这些类别包括worker, company, test.

这是我的代码:

$faq = new WP_Query(array(
          \'post_type\'=>\'faq\',
          \'order\' => \'DESC\',
          \'category_name\' => \'test\',
        ));
  while($faq->have_posts()) : $faq->the_post();
这应该向我展示测试类别下的所有帖子,但它并没有这样做。我也试过了cat => (id) 但仍然没有结果。循环始终为空。

以下是注册功能:

function faq_post_type() {

// these are the labels in the admin interface, edit them as you like
$labels = array(
    \'name\'                => _x( \'FAQs\', \'Post Type General Name\', \'faq\' ),
    \'singular_name\'       => _x( \'FAQ\', \'Post Type Singular Name\', \'faq\' ),
    \'menu_name\'           => __( \'FAQ\', \'faq\' ),
    \'parent_item_colon\'   => __( \'Parent Item:\', \'faq\' ),
    \'all_items\'           => __( \'All Items\', \'faq\' ),
    \'view_item\'           => __( \'View Item\', \'faq\' ),
    \'add_new_item\'        => __( \'Add New FAQ Item\', \'faq\' ),
    \'add_new\'             => __( \'Add New\', \'faq\' ),
    \'edit_item\'           => __( \'Edit Item\', \'faq\' ),
    \'update_item\'         => __( \'Update Item\', \'faq\' ),
    \'search_items\'        => __( \'Search Item\', \'faq\' ),
    \'not_found\'           => __( \'Not found\', \'faq\' ),
    \'not_found_in_trash\'  => __( \'Not found in Trash\', \'faq\' ),
);
$args = array(
    // use the labels above
    \'labels\'              => $labels,
    // we\'ll only need the title, the Visual editor and the excerpt fields for our post type
    \'supports\'            => array( \'title\', \'editor\', \'excerpt\', ),
    // we\'re going to create this taxonomy in the next section, but we need to link our post type to it now
    \'taxonomies\'          => array( \'faq_tax\' ),
    // make it public so we can see it in the admin panel and show it in the front-end
    \'public\'              => true,
    // show the menu item under the Pages item
    \'menu_position\'       => 20,
    // show archives, if you don\'t need the shortcode
    \'has_archive\'         => true,
);
register_post_type( \'faq\', $args );

}
// hook into the \'init\' action
add_action( \'init\', \'faq_post_type\', 0 );
和类别:

function faq_post_category() {

// again, labels for the admin panel
$labels = array(
    \'name\'                       => _x( \'FAQ Categories\', \'Taxonomy General Name\', \'faq\' ),
    \'singular_name\'              => _x( \'FAQ Category\', \'Taxonomy Singular Name\', \'faq\' ),
    \'menu_name\'                  => __( \'FAQ Categories\', \'faq\' ),
    \'all_items\'                  => __( \'All FAQ Cats\', \'faq\' ),
    \'parent_item\'                => __( \'Parent FAQ Cat\', \'faq\' ),
    \'parent_item_colon\'          => __( \'Parent FAQ Cat:\', \'faq\' ),
    \'new_item_name\'              => __( \'New FAQ Cat\', \'faq\' ),
    \'add_new_item\'               => __( \'Add New FAQ Cat\', \'faq\' ),
    \'edit_item\'                  => __( \'Edit FAQ Cat\', \'faq\' ),
    \'update_item\'                => __( \'Update FAQ Cat\', \'faq\' ),
    \'separate_items_with_commas\' => __( \'Separate items with commas\', \'faq\' ),
    \'search_items\'               => __( \'Search Items\', \'faq\' ),
    \'add_or_remove_items\'        => __( \'Add or remove items\', \'faq\' ),
    \'choose_from_most_used\'      => __( \'Choose from the most used items\', \'faq\' ),
    \'not_found\'                  => __( \'Not Found\', \'faq\' ),
);
$args = array(
    // use the labels above
    \'labels\'                     => $labels,
    // taxonomy should be hierarchial so we can display it like a category section
    \'hierarchical\'               => true,
    // again, make the taxonomy public (like the post type)
    \'public\'                     => true,
);
// the contents of the array below specifies which post types should the taxonomy be linked to
register_taxonomy( \'faq_category\', array( \'faq\' ), $args );

}

// hook into the \'init\' action
add_action( \'init\', \'faq_post_category\', 0 );

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

您的查询应该如下所示(或多或少)

$args = array(
    \'post_type\' => \'FAQ \',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'faq_category\',
            \'field\'    => \'slug\',
        ),
    ),
);
$query = new WP_Query( $args );

结束