从 Wordpress 取消注册自定义帖子类型

Posted

技术标签:

【中文标题】从 Wordpress 取消注册自定义帖子类型【英文标题】:Unregister custom post type from Wordpress 【发布时间】:2016-08-13 11:43:27 【问题描述】:

我正在尝试删除通过 Wordpress 中的不同主题设置的自定义帖子类型,现在所有这些帖子都分配给 portfoliopost_type。经过大量搜索,我找到了下面的代码,但它似乎不起作用。我尝试将它添加到新主题和旧主题functions.php

我想删除 post_type 并将帖子分类并显示为普通帖子。我认为我所做的是正确的,但似乎无法让它工作 - 我已经发布了自定义帖子类型的代码和取消注册分配给它的帖子的代码。

注册帖子类型的代码

if ( ! function_exists( 'unregister_post_type' ) ) :
function unregister_post_type() 
global $wp_post_types;
if ( isset( $wp_post_types[ 'portfolio' ] ) ) 
    unset( $wp_post_types[ 'portfolio' ] );
    return true;

return false;

endif;

add_action('init', 'unregister_post_type');

注册帖子类型的代码

register_post_type( 'portfolio',
    array(
        'labels' => array(
             'name' => __('Portfolio Items'),
             'singular_name' => __('Portfolio Item'),
             'add_new_item' => __('Add New Portfolio Item'),
             'edit_item' => __('Edit Portfolio Item'),
             'new_item' => __('New Portfolio Item'),
             'view_item' => __('View Portfolio Item'),
             'search_items' => __('Search Portfolio Items'),
             'not_found' => __('No portfolio items found'),
             'not_found_in_trash' => __('No portfolio items found in Trash')
        ),
        'public' => true,
        'show_ui' => true,
        'hierarchical' => false,
        'menu_position' => 7,
        //'rewrite' => array('slug' => 'portfolio'),
        'rewrite' => true,
        '_built_in' => false,
        'taxonomies' => array( 'post_tag','category','portfolio_tag', 'portfolio_category', 'client'),
        'supports' => array( 'title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions')
    )
);

【问题讨论】:

【参考方案1】:

这是在 wordpress 中取消注册自定义帖子类型的代码。请记住,您需要首先从注册您的帖子类型的函数中清除您的functions.php。

if( !function_exists( 'prefix_unregister_post_type' ) ) 
  function prefix_unregister_post_type() 
    unregister_post_type( 'portfolio' );
  

add_action('init','prefix_unregister_post_type');

【讨论】:

【参考方案2】:

我可以使用以下代码在 WordPress 4.6.1 中删除它:

function delete_post_type()
  unregister_post_type( 'portfolio' );

add_action('init','delete_post_type', 100);

【讨论】:

【参考方案3】:

您的代码看起来不错!但是,如果您取消注册 post_type ......其中的帖子会消失......所以不要过早取消注册。在取消注册帖子类型之前,请将帖子从 post_type 迁移到普通帖子。这个插件很方便:https://wordpress.org/plugins/post-type-switcher/

但如果您不想将视频帖子迁移到默认帖子...您必须修改循环以包含那些投资组合类型的帖子:

function add_custom_post_type_to_query( $query ) 
    if ( $query->is_home() && $query->is_main_query() ) 
        $query->set( 'post_type', array('post', 'portfolio') );
    

add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

&在使用自定义帖子类型时不要忘记访问永久链接页面,以使 WordPress 识别您所做的更改。

【讨论】:

【参考方案4】:

试试这个代码(优先级大)

function custom_unregister_theme_post_types() 
    global $wp_post_types;

      if ( isset( $wp_post_types["portfolio"] ) ) 
         unset( $wp_post_types[ "portfolio" ] ); //UPDATED
      


add_action( 'init', 'custom_unregister_theme_post_types', 20 );

注意:确保注册的帖子类型名称为portfolioportfolios(带s),register_post_type($post_type, $args)

更新: unset( $wp_post_types[ "portfolio" ] ); //UPDATED

【讨论】:

不幸的是仍然无法正常工作。我将它添加到新主题和注册帖子类型的旧主题的functions.php中。 它来自不同的主题,你能把init改成after_setup_theme吗?如果从任何插件使用 plugins_loaded 钩子。 另外,请参阅this 帖子。

以上是关于从 Wordpress 取消注册自定义帖子类型的主要内容,如果未能解决你的问题,请参考以下文章

PHP Wordpress自定义帖子类型注册

WordPress 自定义帖子类型单一作为存档的父级

php Wordpress:注册新的自定义帖子类型

php 显示wordpress注册的自定义帖子类型列表

Wordpress 自定义帖子类型类别

WordPress > 从自定义帖子类型获取自定义分类