admin管理员组文章数量:1022712
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.
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.
Share Improve this question edited May 1, 2019 at 9:15 norman.lol 3,2413 gold badges30 silver badges35 bronze badges asked Apr 30, 2019 at 18:36 Dan SutherlandDan Sutherland 1632 gold badges2 silver badges11 bronze badges1 Answer
Reset to default 3Registering 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
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.
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.
Share Improve this question edited May 1, 2019 at 9:15 norman.lol 3,2413 gold badges30 silver badges35 bronze badges asked Apr 30, 2019 at 18:36 Dan SutherlandDan Sutherland 1632 gold badges2 silver badges11 bronze badges1 Answer
Reset to default 3Registering 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
本文标签: authorHow is gettheauthor connected to a custom post type
版权声明:本文标题:author - How is get_the_author connected to a custom post type? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745536154a2154969.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论