使用自定义帖子类型的自定义分类找不到帖子

时间:2014-02-28 作者:Jonathan

我创建了一个名为mfg_ppn 还有一种自定义分类法mfg_pp, 它们被引用为$this->cpt_promotion_news$this->cpt_promotion如果你想知道的话。

管理中的一切看起来都很好,我可以轻松地将分类法分配给自定义帖子。但是当我使用http://example.com/promotion/company 我没有得到任何职位,即使有12个职位绑定到该特定术语。

下面是我用于注册帖子类型的PHP代码:

// Register campaign news:
register_post_type($this->cpt_promotion_news, array(
    \'labels\' => array(
        \'name\' => __(\'Campaign Posts\', $this->plugin_locale),
        \'singular_name\' => __(\'Promotion Campaign\', $this->plugin_locale),
        \'menu_name\' => __(\'Promotion\', $this->plugin_locale),
        \'all_items\' => __(\'All Campaign Posts\', $this->plugin_locale),
        \'add_new\' => __(\'Add New Post\', $this->plugin_locale),
        \'add_new_item\' => __(\'Add New Campaign Post\', $this->plugin_locale),
        \'edit_item\' => __(\'Edit Post\', $this->plugin_locale),
        \'new_item\' => __(\'New Post\', $this->plugin_locale),
        \'view_item\' => __(\'View Post\', $this->plugin_locale),
        \'search_items\' => __(\'Search Posts\', $this->plugin_locale),
        \'not_found\' => __(\'No posts found\', $this->plugin_locale),
        \'not_found_in_trash\' => __(\'No posts found in trash\', $this->plugin_locale),
        \'parent_item_colon\' => __(\'Post parent:\', $this->plugin_locale)
    ),
    \'description\' => __(\'News for a partner promotion campaign.\', $this->plugin_locale),
    \'public\' => true,
    \'exclude_from_search\' => true,
    \'show_in_nav_menus\' => true,
    \'menu_position\' => 25,
    \'show_in_menu\' => true,
    \'map_meta_cap\' => true,
    \'capabilities\' => array( \'manage_options\' ),
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,
    \'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'comments\' ),
    \'taxonomies\' => array( $this->cpt_promotion ),
    \'has_archive\' => true,
    \'rewrite\' => array(
        \'slug\' => __(\'promotion-post\', $this->plugin_locale)
    )
));
下面是注册分类法的代码:

// Register promotion campaigns:
register_taxonomy($this->cpt_promotion, $this->cpt_promotion_news, array(
    \'labels\' => array(
        \'name\' => __(\'Promotion Campaigns\', $this->plugin_locale),
        \'singular_name\' => __(\'Promotion Campaign\', $this->plugin_locale),
        \'menu_name\' => __(\'All Campaigns\', $this->plugin_locale),
        \'all_items\' => __(\'All Campaigns\', $this->plugin_locale),
        \'edit_item\' => __(\'Edit Campaign\', $this->plugin_locale),
        \'view_item\' => __(\'View Campaign\', $this->plugin_locale),
        \'update_item\' => __(\'Update Campaign\', $this->plugin_locale),
        \'add_new_item\' => __(\'Add New Promotion Campaign\', $this->plugin_locale),
        \'new_item_name\' => __(\'New Campaign Name\', $this->plugin_locale),
        \'parent_item\' => __(\'Post parent:\', $this->plugin_locale),
        \'parent_item_colon\' => __(\'Post parent:\', $this->plugin_locale),
        \'search_items\' => __(\'Search Campaigns\', $this->plugin_locale),
        \'add_or_remove_items\' => __(\'Add or Remove Campaigns\', $this->plugin_locale)
    ),
    \'public\' => true,
    \'show_ui\' => true,
    \'show_in_nav_menus\' => false,
    \'show_tagcloud\' => false,
    \'show_admin_column\' => true,
    \'hierarchical\' => true,
    \'query_var\' => true,
    \'rewrite\' => array(
        \'slug\' => __(\'promotion\', $this->plugin_locale)
    )
));
我还创建了一个名为taxonomy-mfg_pp, 这是分类法的名称,但它从不输出任何帖子,即使有帖子。罪魁祸首似乎是执行的WP\\U查询只查找帖子、页面和附件,如下所示:

SELECT SQL_CALC_FOUND_ROWS  qADrathuFrU2_posts.ID FROM qADrathuFrU2_posts  INNER JOIN qADrathuFrU2_term_relationships ON (qADrathuFrU2_posts.ID = qADrathuFrU2_term_relationships.object_id) WHERE 1=1  AND ( qADrathuFrU2_term_relationships.term_taxonomy_id IN (1196) ) AND qADrathuFrU2_posts.post_type IN (\'post\', \'page\', \'attachment\') AND (qADrathuFrU2_posts.post_status = \'publish\' OR qADrathuFrU2_posts.post_author = 1 AND qADrathuFrU2_posts.post_status = \'private\') AND qADrathuFrU2_posts.post_password = \'\'  GROUP BY qADrathuFrU2_posts.ID ORDER BY qADrathuFrU2_posts.post_date DESC LIMIT 0, 10
我是不是做错了什么,我是不是走错了路?

请帮帮我,因为最后期限快到了,我似乎做不好!

提前谢谢你Jonathan

2 个回复
最合适的回答,由SO网友:Jonathan 整理而成

我不知道如何或为什么,但下面的代码解决了这个问题。在我看来,我不应该需要它,但显然我需要。

add_filter(\'pre_get_posts\', array(&$this, \'modify_pre_query_request\'));
public function modify_pre_query_request($query){
    if ($query->is_main_query()){
        if ($query->is_tax){
            $post_type = get_query_var(\'post_type\');
            if (!$post_type){
                $post_type = array( \'post\', \'YOUR POST TYPE\' );
                $query->set(\'post_type\', $post_type);
            }
        }
    }
}
请记住,上述代码适用于面向对象主题,如果您不使用面向对象主题,请使用以下代码:

add_filter(\'pre_get_posts\', \'modify_pre_query_request\');
function modify_pre_query_request($query){
    if ($query->is_main_query()){
        if ($query->is_tax){
            $post_type = get_query_var(\'post_type\');
            if (!$post_type){
                $post_type = array( \'post\', \'YOUR POST TYPE\' );
                $query->set(\'post_type\', $post_type);
            }
        }
    }
}

Thanks for all the help!

SO网友:Amin

非常感谢Jonathan的回答this answer, 这可能是一种更灵活的解决此问题的方法,因为它会自动确定帖子类型,您无需担心帖子类型名称:

add_action( \'pre_get_posts\', \'dw_handle_posts\' );
function dw_handle_posts( $query ) {
    if( ! $query->is_main_query() || is_admin() )
        return;

    if ( $query->is_tax ){
        $post_type = get_query_var(\'post_type\');
        if( ! $post_type ){
            global $wp_taxonomies;
            $taxo = get_queried_object();
            $post_type = ( isset( $wp_taxonomies[$taxo->taxonomy] ) ) ? $wp_taxonomies[$taxo->taxonomy]->object_type : array();
            $query->set(\'post_type\', $post_type);
        }
    }

    return $query;
}

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register