简体   繁体   English

WordPress:为什么get_terms()不能看到在类中注册的自定义分类法?

[英]WordPress: Why Can't get_terms() See Custom Taxonomy Registered inside Class?

Background 背景

I register a custom post type and custom taxonomy inside a class. 我在类中注册了自定义帖子类型和自定义分类法。 Inside the WP admin, I see both the post type, and I see the taxonomy. 在WP管理员内部,我既看到了职位类型,又看到了分类法。

Simplified Code: 简化代码:

class Custom_Post_Type {

    function __construct($init_data) {

        if ( is_admin() ) {

            add_action( 'init', array( $this, 'create_ctp' ) );
            add_action( 'admin_head', array( $this, 'create_ctp_icons' ) );
            add_action( 'save_post', array( $this, 'save_ctp_custom_metadata' ), 1, 2 );

        }

    }

    function create_ctp_taxonomy() {
            register_taxonomy(
                $post_type.'_type',
                $post_type,
                array(
                    'labels' => array(
                        'name' => $taxonomy_label,
                        'add_new_item' => 'Add New '.$taxonomy_label
                    ),
                    'public' => true,
                    'show_ui' => true,
                    'show_tagcloud' => true,
                    'hierarchical' => true,
                    'show_in_nav_menus' => true,
                    'show_admin_column' => true
                )
            );

            register_post_type($post_type_slug,
                array(
                    'labels' => array(),
                    'public' => true,
                    'has_archive' => false,
                    'supports' => $this->supports,
                    'register_meta_box_cb' => array( $this, 'create_ctp_custom_metaboxes' ),
                    'taxonomies' => array( $taxonomy_slug ),
                )
            );
    }

}

Again, this works inside the admin area. 同样,这在管理区域内有效。 I can add posts, and I can see the taxonomy and add terms. 我可以添加帖子,也可以看到分类法并添加条款。


Problem 问题

On the front end, get_taxonomies() doesn't see the new custom taxonomy, and get_terms() doesn't see the terms inside it. 在前端, get_taxonomies()没有看到新的自定义分类法,而get_terms()没有看到其中的术语。

I tried several examples of of register_taxonomy , and when I used it outside of the class, it appears on the front end. 我尝试了register_taxonomy几个示例,当我在类之外使用它时,它出现在前端。 However, when I moved the examples into my internal create_ctp_taxonomy function, they are suddenly invisible to get_taxonomies . 但是,当我将示例移到内部create_ctp_taxonomy函数中时,它们对于get_taxonomies突然不可见。

Any ideas why this would be occurring? 任何想法为什么会这样?


Edit 编辑

I've been playing around with different things and I think the issue here is the timing of the init action. 我一直在玩各种不同的事情,我认为这里的问题是init动作的时机。 When I call the setup function direction from the __construct function, rather than adding an action, then things start working. 当我从__construct函数调用安装函数方向时,而不是添加操作,事情就开始起作用了。 Example: 例:

class Custom_Post_Type {

    function __construct($init_data) {

        if ( is_admin() ) {

            //add_action( 'init', array( $this, 'create_ctp' ) );
            add_action( 'admin_head', array( $this, 'create_ctp_icons' ) );
            add_action( 'save_post', array( $this, 'save_ctp_custom_metadata' ), 1, 2 );
        }

        $this->create_cpt();

    }

}

By doing this, I skip using init at all. 通过这样做,我完全跳过了使用init的过程。 However, this seems to violate standard practice. 但是,这似乎违反了标准做法。 Is there a downside that anyone knows of for doing it this way? 任何人都知道这样做有不利之处吗?

There are a couple of things you need to be aware of when registering taxonomies to custom post types. 将分类法​​注册到自定义帖子类型时,需要注意几件事。

  1. Register the taxonomies first. 首先注册分类法。 This seems a bit counter intuitive but taxonomies are registered to post types, so they need to exist before the post type is created. 这似乎有点反直觉,但分类法已注册到帖子类型,因此在创建帖子类型之前,它们必须存在。
  2. You also need to register the taxonomy to the post type using the taxonomies argument of the register_post_type function. 您还需要使用register_post_type函数的taxonomies参数将分类法注册为帖子类型。

Eg. 例如。

 register_post_type('cpt_name',array(
        'taxonomies'=>array(
              'taxomony_name1',
              'taxomony_name2')
         ,other_arguments...)
 );

From the docs 来自文档

When registering a post type, always register your taxonomies using the taxonomies argument. 注册帖子类型时,请始终使用taxonomies参数注册您的分类法。 If you do not, the taxonomies and post type will not be recognized as connected when using filters such as parse_query or pre_get_posts. 如果不这样做,则在使用诸如parse_query或pre_get_posts之类的过滤器时,分类法和帖子类型将不会被识别为已连接。 This can lead to unexpected results and failures 这可能会导致意外结果和故障

Not Problems 没问题

1.) The problem isn't a race condition. 1.)问题不是比赛条件。

Conditionals like is_admin() still work when run from functions.php directly. 直接从functions.php运行时,诸如is_admin()类的条件仍然有效。 This contradicts some information on the web, but as of WordPress 4.4, these do work. 这与网络上的某些信息相矛盾,但是从WordPress 4.4开始,这些信息确实起作用。

2.) Calling the registration from add_action() rather than directly from __construct() 2.)从add_action()而不是直接从__construct()调用注册

Switching to calling the registration directly had zero change. 直接切换到调用注册的更改为零。 To be clear, there is no difference between: 需要明确的是,两者之间没有区别:

  • $this->create_ctp()

  • add_action('init', array( $this, 'create_ctp' ) );

3.) Order of registering taxonomy vs CTP 3.)分类法与CTP的注册顺序

When I moved my registration of the taxonomy in front of the CTP, it had zero change in behavior. 当我将分类法注册移到CTP之前时,其行为更改为零。


The Problem 问题

I was using a conditional check of is_admin() , which I'd added previous to wrap when I added the admin dashicon. 我正在使用is_admin()的条件检查,这是我在添加admin破折号时先前添加的要包装的内容。 This is why my CTP was appearing on the backend, but not on the front. 这就是为什么我的CTP出现在后端而不是前端的原因。

I had removed it from my simplified example, so there was no way to tell from looking at the code that I'd posted. 我已经从简化的示例中删除了它,因此无法通过查看我发布的代码来分辨。

So all I needed to do was remove the is_admin() check. 因此,我要做的就是删除is_admin()检查。 A silly mistake it turns out, but useful to find the information about what things aren't actually problems. 事实证明这是一个愚蠢的错误,但对于查找实际上不是问题的信息很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM