使用register_post_type()修改现有的帖子类型


29

在许多情况下,主题或插件会注册帖子类型,而您想对其进行修改。当然有add_post_type_support()remove_post_type_support(),但是那些没有提供访问所采用参数的完整列表的权限register_post_type()。特别是,也许我想禁用帖子类型存档,隐藏管理用户界面,从搜索中隐藏等,同时不考虑其余的帖子类型设置。

法典页面register_post_type()在我面前的这个悬点:

描述

创建或修改帖子类型。

但是在过去,当我尝试执行此操作时,它似乎没有起作用。此函数是否真的可以修改帖子类型,如果可以,您是否可以简单地重新声明几个参数,而将其余的保留下来?

看到甚至没有一个deregister_post_type()功能,我不知道该怎么做。

Answers:


18

这个功能真的可以修改帖子类型吗

是。

如果是这样,您能否简单地重新声明几个论点而将其余论点放在一边?

否。如果您想将参数修改为帖子类型,则需要使用它get_post_type_object来获取帖子类型对象,修改您想要的内容,然后使用修改后的类型作为新的$ args参数重新注册它。


用相同的修改参数连续两次调用register_post_type是否正确?我以您的“是”为前提,并且它不会转储任何错误并具有预期的效果。真实案例是我在此答案中提出的第二个选项:wordpress.stackexchange.com/a/74331/12615
brasofilo 2012年

是的,这行得通,但是好像您必须这样做,那么您需要添加一些过滤器或其他工具,以防止不得不一遍又一遍地重复注册相同的帖子类型。基本上,首先整理参数,然后注册。
奥托(Otto)

我只是考虑了一下,因为插件的前面提供了钩子register_post_type。实际上,没有必要。这是一个“用于记录”的问题,感谢您的反馈。
brasofilo 2012年

34

经过一些研究,我发现这些答案都不是最新的。

自2015年12月8日起,WordPress包括一个新的过滤器,register_post_type_args您可以使用它来注册已注册帖子类型的参数。

function wp1482371_custom_post_type_args( $args, $post_type ) {
    if ( $post_type == "animal-species" ) {
        $args['rewrite'] = array(
            'slug' => 'animal'
        );
    }

    return $args;
}
add_filter( 'register_post_type_args', 'wp1482371_custom_post_type_args', 20, 2 );

6
这是现在正确的方法。
Dave Romsey

6
这应该是公认的答案
klewis

如果在运行时中注册后需要更新该怎么办?例如:之后init
Lucas Bustamante

@LucasBustamante没关系。只需在插件/主题过程中调用“ add_filter”行即可,而无需执行任何操作。过滤器将在每个register_post_type函数中调用。我的代码不起作用的唯一情况是,如果帖子类型未正确注册。所有帖子类型都应在初始化挂钩期间进行注册。从法典中:“如果在'init'之前调用,register_post_type将不起作用,而在以后调用时,新创建或修改的帖子类型的各个方面将无法正常工作。“ 如果您仍然需要帮助,建议您在新问题中发布更多详细信息。
Radley Sustaire

8

这是一个示例,说明如何使用'registered_post_type'过滤器修改另一个插件中的帖子类型。

我使用的插件的定义中没有包含menu_icon,因此我想添加一个自己的插件。

<?php
/**
 * Add a menu icon to the WP-VeriteCo Timeline CPT
 *
 * The timeline plugin doesn't have a menu icon, so we hook into 'registered_post_type'
 * and add our own.
 *
 * @param  string $post_type the name of the post type
 * @param  object $args the post type args
 */
function wpse_65075_modify_timeline_menu_icon( $post_type, $args ) {
    // Make sure we're only editing the post type we want
    if ( 'timeline' != $post_type )
        return;

    // Set menu icon
    $args->menu_icon = get_stylesheet_directory_uri() . '/img/admin/menu-timeline.png';

    // Modify post type object
    global $wp_post_types;
    $wp_post_types[$post_type] = $args;
}
add_action( 'registered_post_type', 'wpse_65075_modify_timeline_menu_icon', 10, 2 );

在大多数情况下,这是一种更清洁的方法。但是,应该注意的是,此过滤器为时已晚,无法被重写使用,因此在这种情况下必须使用可接受的答案。
mrwweb

4

'registered_post_type'在其他代码将其注册后挂入。在的结尾称为register_post_type()。您有两个参数:$post_type$args
现在,您可以更改此帖子类型的任何内容。检查$GLOBALS['wp_post_types']某些选项。


感谢您将我指向该挂钩。这回答了有关如何修改帖子类型的(更重要的)问题,但是对它的描述register_post_type()包括“修改”呢?那是错的吗?我现在该不该跳上互联网马并从食品法典委员会手中夺走它?
mrwweb 2012年

2
$wp_post_types[$post_type] = $args;…建议您应该能够更改属性。将失败代码的示例添加到您的问题中。
fuxia

0

我使用“事件日历”插件遇到了同样的事情。

我将以下代码添加到function.php以修改tribe_organizer帖子类型

function tribe_modify_organizer() {
 //New arguments
    $tribe_organizer_args = get_post_type_object('tribe_organizer'); // get the post type to modify
    $tribe_organizer_args-> taxonomies = array('post_tag' , 'tribe_events_cat'); // add taxonomies support
    $tribe_organizer_args-> exclude_from_search = false; // show in search result
 //re-register the same post type includeing the new args
    register_post_type( 'tribe_organizer', $tribe_organizer_args );
}
add_action( 'init', 'tribe_modify_organizer', 100 );

0

我不知道这是否难看,但是GLOBAL只要需要操纵单个参数,就可以“即时” 更改占位符。这就是我们contents在管理员菜单中使用非公开帖子类型的方式。我们在菜单渲染之前挂接close和关闭之后:

function entex_theme_make_contents_public(){
    $GLOBALS['wp_post_types']['contents']->public = true;
}
add_action('admin_menu', 'entex_theme_make_contents_public', 10);

function entex_theme_make_contents_private_again(){
    $GLOBALS['wp_post_types']['contents']->public = '';
}
add_action('admin_menu', 'entex_theme_make_contents_private_again', 12);

在我们的例子中,我们希望Admin Menu Post List插件接受我们的帖子类型,因为他们return get_post_types(array('public' => true));在优先级为11的钩子中调用...

开发人员-如果可能引起任何问题,请发表评论。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.