-1

I am having issues with certain concepts regarding Wordpress and it's development, if anyone could clarify things for me that would be great!

I have an understanding of PHP classes and an understanding of how to generate and work with Wordpress Custom Post Types, are CPT's objects of a class i.e. is there a 'Post' object and custom post types are just objects of the class?

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.

I am presuming that this is because it is of the post object and the author is a property set by a setter when ANY post type is created? but this is where my understanding falls down a little.

3

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 post
  • WP_Query grabs multiple posts given some arguments and lets you loop over them in a posts loop
  • wp_insert_post and wp_update_post can create and update posts
  • wp_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

  • Thank you Tom for taking the time to pop an explanation down, that is REALLY helpful, I appreciate it. – Dan Sutherland Apr 30 at 21:17
  • 1
    @DanSutherland – If you find this answer helpful please consider upvoting and accepting it. Thank you comments are discouraged. Many thanks – leymannx May 1 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.