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

     2023-02-24     73

关键词:

【中文标题】从 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 自定义帖子类型类别

】Wordpress自定义帖子类型类别【英文标题】:Wordpresscustomposttypecategories【发布时间】:2011-06-2412:07:27【问题描述】:嘿。我在wordpress中使用自定义帖子类型。我像这样注册这个自定义帖子类型:register_post_type("lifestream",array(\'label... 查看详情

php显示wordpress注册的自定义帖子类型列表(代码片段)

查看详情

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

】WordPress>从自定义帖子类型获取自定义分类【英文标题】:WordPress>Getcustomtaxonomyfromacustomposttype【发布时间】:2016-10-2805:24:14【问题描述】:InstalledCustomPostTypeUIwordpressplugin创建了自定义帖子类型=\'products\'使用注册了一个自... 查看详情

用于自定义帖子类型的 Wordpress 多个 slug

】用于自定义帖子类型的Wordpress多个slug【英文标题】:WordpressmultipleslugsforaCustomPostType【发布时间】:2015-08-1212:49:39【问题描述】:可以使用一个slug注册自定义帖子类型(例如产品)register_post_type(\'products\',$args);如何将多个slug... 查看详情

WordPress 自定义帖子类型前端特色图片提交

】WordPress自定义帖子类型前端特色图片提交【英文标题】:WordPressCustomPostTypeFrontEndFeaturedImageSubmission【发布时间】:2021-12-2608:53:01【问题描述】:我有一个表单可以从前端提交自定义帖子类型。但是图像提交不起作用。我认为附... 查看详情

从 Wordpress 中自定义帖子类型的类别中获取 ACF 文本字段值

】从Wordpress中自定义帖子类型的类别中获取ACF文本字段值【英文标题】:GettheACFtextfieldvaluefromthecategoryofacustomposttypeinWordpress【发布时间】:2020-10-2520:00:23【问题描述】:我正在尝试使用ACFPRO中的文本字段的值创建另一个类。我有... 查看详情

如何在 wordpress 中显示自定义帖子类型类别?

】如何在wordpress中显示自定义帖子类型类别?【英文标题】:Howtodisplaycustomposttypecategoryinwordpress?【发布时间】:2014-04-1416:13:21【问题描述】:最近我在wordpress中创建了一个自定义帖子类型,并添加了名为category和tags的分类法。... 查看详情

Wordpress - 从自定义帖子类型中删除子菜单

】Wordpress-从自定义帖子类型中删除子菜单【英文标题】:Wordpress-Removesubmenufromcustomposttype【发布时间】:2011-09-3012:58:55【问题描述】:我创建了一个名为投资组合的自定义帖子类型,支持标签分类。由于WP在帖子标签和自定义帖... 查看详情

没有自定义帖子类型 slug 的 WordPress slug

】没有自定义帖子类型slug的WordPressslug【英文标题】:WordPressslugwithoutcustomposttypeslug【发布时间】:2017-01-3123:39:54【问题描述】:我已经使用以下代码注册了自定义帖子类型:register_post_type(array(\'labels\'=>//arrayoflabels,\'public\'=>... 查看详情

Wordpress 为自定义帖子类型存档页面选择了错误的模板

】Wordpress为自定义帖子类型存档页面选择了错误的模板【英文标题】:WordpressPicksUpWrongTemplateforCustomPostTypeArchivePage【发布时间】:2017-11-2111:31:28【问题描述】:我已经注册了以下两种自定义帖子类型:functiondogs()$labels=array(\'name\'... 查看详情

Wordpress ACF 字段如何从自定义帖子类型中获取选项

】WordpressACF字段如何从自定义帖子类型中获取选项【英文标题】:WordpressACFfieldshowtoget_optionfromcustomposttypes【发布时间】:2021-07-2221:37:19【问题描述】:我使用ACF并有一个名为“关于”的页面模板,我的“关于我们”页面使用该... 查看详情

Wordpress - 我如何从其他自定义帖子中获取_categories?

】Wordpress-我如何从其他自定义帖子中获取_categories?【英文标题】:Wordpress-HowcanIget_categoriesfromadditionalcustompost?【发布时间】:2022-01-1905:41:30【问题描述】:我的目标是从我在functions.php中注册的附加自定义帖子中获取类别。如何... 查看详情

按自定义分类类型迭代自定义帖子类型? (按类别排序 wordpress 帖子,或按分类术语显示自定义帖子类型)

】按自定义分类类型迭代自定义帖子类型?(按类别排序wordpress帖子,或按分类术语显示自定义帖子类型)【英文标题】:Iteratethroughcustomposttypebycustomtaxonomytype?(Orderingwordpresspostsbycategory,ordisplayingcustomposttypebytaxonomyterm)【发布时... 查看详情

Wordpress 自定义帖子类型和自定义分类

】Wordpress自定义帖子类型和自定义分类【英文标题】:WordpressCustomPostTypeandCustomTaxonomy【发布时间】:2013-08-0314:50:08【问题描述】:我在自定义帖子类型和自定义分类方面遇到问题。我创建了一个名为“gallery”的自定义帖子类型... 查看详情

Wordpress 过滤自定义帖子类型分类

】Wordpress过滤自定义帖子类型分类【英文标题】:Wordpressfilteringcustomposttypetaxonomy【发布时间】:2020-11-1719:01:03【问题描述】:我一直在关注本指南:https://rudrastyh.com/wordpress/ajax-post-filters.html#comment-908。我有一个名为“project”的... 查看详情

Wordpress - 自定义帖子类型存档页面

】Wordpress-自定义帖子类型存档页面【英文标题】:Wordpress-Customposttypearchivepage【发布时间】:2014-10-0719:13:24【问题描述】:我使用自定义帖子类型UI插件创建了一个自定义帖子类型,并使其支持存档。我有一个名为列表的CPT。我... 查看详情

自定义帖子类型等效于 Wordpress 中的“帖子页面”设置

】自定义帖子类型等效于Wordpress中的“帖子页面”设置【英文标题】:CustomPostTypeequivalentfor"PostsPage"settinginWordpress【发布时间】:2013-10-1709:11:33【问题描述】:我正在尝试在wordpress中设置一个自定义帖子类型插件,该插件... 查看详情

获取自定义帖子类型(Wordpress)的每年帖子数量的数组

】获取自定义帖子类型(Wordpress)的每年帖子数量的数组【英文标题】:Getanarrayofthenumberofpostperyearofacustomposttype(Wordpress)【发布时间】:2021-09-2005:38:05【问题描述】:我有一个名为“论文”的自定义帖子类型,为了提供图表,我... 查看详情