問題描述

一直以來,我一直在使用自定義的帖子類型,但是昨天我把自己的第一個分支與他們分開了。

這是我的’company’ 分類:

function create_company_taxonomy() {
    $labels = array(
        'name'              => _x( 'Companies', 'taxonomy general name' ),
        'singular_name'     => _x( 'Company', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Companies' ),
        'all_items'         => __( 'All Companies' ),
        'parent_item'       => __( 'Parent Company' ),
        'parent_item_colon' => __( 'Parent Company:' ),
        'edit_item'         => __( 'Edit Companies' ),
        'update_item'       => __( 'Update Companies' ),
        'add_new_item'      => __( 'Add New Company' ),
        'new_item_name'     => __( 'New Company Name' ),
        'menu_name'         => __( 'Company' ),
    );

    $args = array(
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'company' ),
        'public' => true,
        'hierarchical' => true,
        'show_ui' => true,
        'show_in_nav_menus' => true,
        'query_var' => true,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'has_archive' => true
    );

    register_taxonomy( 'company', array( 'brochures','business-cards','post','websites' ), $args );
}
add_action( 'init', 'create_company_taxonomy', 0 );

你可以看到我正在嘗試使用它與’posts’ 以及三種自定義的職位類型 – ‘brochures’,’business-cards’,’websites’

當我創建一個 taxonomy-company.php 模板 (基於 archive.php),並從分類中給出這些 post_types 中的每一個術語時,只有在頁面上顯示帖子。我無法獲得顯示的自定義帖子類型。

我需要自定義查詢嗎?任何見解將不勝感激。

編輯:這是我的一個 register_post_types

add_action( 'init', 'create_custom_post_types' );
function create_custom_post_types() {

   $labels = array(
    'name' => __( 'Websites' ),
    'singular_name' => __( 'Website' )
    );

    $args = array(
    'labels' => $labels,
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'websites'),
    'taxonomies' => array( 'category','company','post_tag' ),
    'supports'  => array( 'title', 'editor', 'thumbnail' , 'custom-fields', 'excerpt' ),
    'exclude_from_search' => true
    );

  register_post_type( 'websites', $args);
}

最佳解決方案

默認情況下,所有公共帖子類型都包含在分類頁面的主要主要查詢中。如果在設置為 true 時查看 register_post_type()'s 的公共參數

  • ‘true’

  • Implies exclude_from_search: false, publicly_queryable: true, show_in_nav_menus: true, and show_ui:true.

註冊您的帖子類型時,您將 exclude_from_search 設置為 true 。這不僅從搜索中刪除自定義帖子類型,還可以從分類頁面上的主查詢中刪除。再次,從 exclude_from_search 參數的 codex

Note: If you want to show the posts’s list that are associated to taxonomy’s terms, you must set exclude_from_search to false (ie : for call site_domaine/?taxonomy_slug=term_slug or site_domaine/taxonomy_slug/term_slug). If you set to true, on the taxonomy page (ex: taxonomy.php) WordPress will not find your posts and/or pagination will make 404 error.

解決方案

您需要將 exclude_from_search 設置為 false,重新填充您的永久鏈接和您的好友。如果您需要從搜索頁面中排除帖子類型,可以使用 pre_get_posts 從主查詢中刪除帖子類型

EDIT

只是為了興趣,這裏是 WP_Query 類 (在 wp-includes /query.php) 中的代碼,它負責從分類術語歸檔頁面的主查詢中包括和排除發佈類型

if ( $this->is_tax ) {
    if ( empty($post_type) ) {
        // Do a fully inclusive search for currently registered post types of queried taxonomies
        $post_type = array();
        $taxonomies = array_keys( $this->tax_query->queried_terms );
        foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
            $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
            if ( array_intersect( $taxonomies, $object_taxonomies ) )
                $post_type[] = $pt;
        }
        if ( ! $post_type )
            $post_type = 'any';
        elseif ( count( $post_type ) == 1 )
            $post_type = $post_type[0];

        $post_status_join = true;
    } elseif ( in_array('attachment', (array) $post_type) ) {
        $post_status_join = true;
    }
}

這裏的重要部分是:get_post_types( array( 'exclude_from_search' => false )。在這裏,查詢獲取 exclude_from_search 設置為 false 的所有公共類型。 exclude_from_search 設置為 true 的所有帖子類型將從查詢中排除,這就是為什麼您的自定義帖子類型不包含在分類術語存檔頁面中

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。