最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

posts - Why does WP_Post not contain its permalink?

programmeradmin4浏览0评论

When I do something like:

$prevPost = get_previous_post();

I can then do:

$prevPost->post_title;
$prevPost->post_content;
...

Why can't I get the permalink?

$prevPost->post_url; // NULL
$prevPost->post_permalink; // NULL

Obviously, as stated in the codex, that class doesn't have these properties. However, it seems logical to have the post URL as part of the post. Why is that not the case?

To get the permalink, I need to do:

get_permalink($prevPost);

But then, I can't do something like this to get the title:

get_title($prevPost);

Questions

  1. What's the reason for these inconsistencies? Why do I have to get some of the post data via the post object and other - with the get functions? I'm relatively new to WordPress, so an article explaining it all would be much appreciated.

  2. Are there any other types of data that share these inconsistencies? What are they?

When I do something like:

$prevPost = get_previous_post();

I can then do:

$prevPost->post_title;
$prevPost->post_content;
...

Why can't I get the permalink?

$prevPost->post_url; // NULL
$prevPost->post_permalink; // NULL

Obviously, as stated in the codex, that class doesn't have these properties. However, it seems logical to have the post URL as part of the post. Why is that not the case?

To get the permalink, I need to do:

get_permalink($prevPost);

But then, I can't do something like this to get the title:

get_title($prevPost);

Questions

  1. What's the reason for these inconsistencies? Why do I have to get some of the post data via the post object and other - with the get functions? I'm relatively new to WordPress, so an article explaining it all would be much appreciated.

  2. Are there any other types of data that share these inconsistencies? What are they?

Share Improve this question asked Jun 30, 2017 at 14:23 dodovdodov 1971 silver badge9 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 3

WP_Post is a representation of the post as it exists in the database, and the permalink isn't stored in the database.

It's not stored in the database because it's not a property of the post. The permalink is determined by the site's address, its permalink structure, and the post's slug. Imagine if updating the permalink structure on your site required that every post in the database had to be updated.

But then, I can't do something like this to get the title:

get_title($prevPost);

You can

get_the_title($prevPost);

For consistency's sake, you can also do:

get_the_permalink($prevPost);

While it's possible to access post content directly from the post object, you should not do this. All data should be accessed via the API functions- title, content, etc., so all applicable filters will be applied. Accessing data directly will break formatting and many plugin functions.

Try

the_permalink($post_object->ID);
发布评论

评论列表(0)

  1. 暂无评论