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 沒有值的時候給它們個默認值。