WordPress 建立自定義文章型別函式 register_post_type 的 public 引數有點難以搞明白,該引數在官網的說明中說:

public

(boolean) (optional) Whether a post type is intended to be used publicly either via the admin interface or by front-end users.

Default: false
  • 'false' - Post type is not intended to be used publicly and should generally be unavailable in wp-admin and on the front end unless explicitly planned for elsewhere.
  • 'true' - Post type is intended for public use. This includes on the front end and in wp-admin.

Note: While the default settings of exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are inherited from public, each does not rely on this relationship and controls a very specific intention.

是否準備讓文章型別在前臺和後臺管理介面公開。設定成 flase 的時候,文章型別將不會被公開使用,設定為 true 的時候,文章型別可以在前臺和後臺公開使用。

當你把 public 設定才 fasle 後,再到後臺看,發現後臺沒什麼變化,訪問新增的該型別文章,也能顯示,切換這個引數的值,沒能發現什麼變化,其實這個引數的說明中後面的 Note 是重點,這個引數的作用算是給另外幾個引數繼承用的,也就是設定了這個引數後,其它幾個引數可以直接繼承這個引數的值,不需另外設定。如果其它引數你都設定了具體的值,這個引數設定成 ture 或 false 都沒什麼關係了。

你也可以檢視該 register_post_type 函式的原始碼,在 wp-includes/post.php 檔案中,可以看到 public 引數只是在其它引數:exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are inherited from public 沒有值的時候給它們個預設值。