Registering a custom post type has nothing to do with object oriented programming and classes.
In the database, there is a column named post_type
, some posts are of type post
, some page
, a custom post type is when you call register_post_type
to tell WP how to handle a new value.
But beyond that, there are no PHP classes unless you create one ( and if you did, I’m not sure what you’d have it do ).
If you pass a post ID to an API such as get_the_author
it knows the author because posts have authors, and there’s an author column in the post table.
As for posts themselves, there’s a generic WP_Post
object that WP creates when it fetches a post from the database, but that object is just a helper. It is not the actual post itself, but just a convenient way to store the data if fetched from the database. There are no special versions of this object for custom post types, and you don’t create posts by creating WP_Post
objects. Modifying one of these objects doesn’t update the database either, it’s not an ORM database layer, nor are getters and setters used, they’re just plain member variables. WP_Post
doesn’t provide any methods for updating the database either.
So, if you want to work with posts:
get_post
gives you a post object if you pass it an ID. If you don’t it assumes you wanted the current postWP_Query
grabs multiple posts given some arguments and lets you loop over them in a posts loopwp_insert_post
andwp_update_post
can create and update postswp_trash_post
trashes a post
There isn’t much OOP in WordPress
The reason I ask is; if I create a new Custom Post Type and use functions such as get_the_author() on my custom post type then the functions work, although I haven’t necessarily defined the author while setting the CPT up.
As I said before, post types are just a field in a database. For WP to know how to deal with them at runtime, it needs to be told via register_post_type
, which tells it about the post type, its user friendly name, does it appear in search results, does it have a URL and archives, etc. Otherwise they’re just posts
Remember, when a request for a webpage ends, everything dissapears, nothing about that PHP request persists other than what’s stored to the filesystem and the database. PHP isn’t like Node or Python where processes handle multiple requests. In PHP a process is created to handle your request then it dies, WP is loaded on every request from scratch