humanmade / humanmade/coding-standards
Do not allow the usage of magic properties on WP_Post objects
- Dominant language
- PHP
- Stars
- 160
- Forks
- 17
- PR merge metrics
- No merged PRs in 30d
Description
The `WP_Post` class offers access to various properties through magic getters:
- `$page_template`
- `$ancestors`
- `$post_category`
- `$tag_input`
In addition, post meta can be retrieved by using `$post->meta_key`.
We should not allow the usage of these coding approaches:
1. Magic properties are discouraged in the PHP community, because IDEs and other code quality tools cannot understand them.
2. Since there are reserved keywords, using `$post->meta_key` does not always return the meta data.
3. Depending on how the post is instantiated, the meta value will be passed through `sanitize_post_field()` before being returned.
4. Meta will always be pulled with `$single` set to `true`.
All in all there are no benefits here, and only downsides.
Contributor guide
Assessment
This issue has not been assessed yet.