The WP_Post Object and Virtual Properties

WordPress 3.5 introduced a lot of changes under the hood. One of the simple things that makes my life much easier is the way that the new WP_Post object allows post meta to be fetched using virtual properties. If you aren’t familiar with classes or virtual properties, all you need to know is that you can use the meta key for your post meta as if it were a property of the post object.

Before WordPress 3.5, the post object was just a stdClass object and the properties were limited to the MySQL fields in the wp_posts database table. Now that it is a WP_Post object, it has been architected to include access to the wp_postmeta table as well using virtual properties.

Comments

  1. This is awesome – thanks. I was looking for a way to attach post meta to the post obj, seems this meets my need. Much cleaner code.

    Only issue, as a dev who works with ACF a lot, and repeater fields, this returns the count of the rows in the repeater field, not the array. Where as, with ACF text field, etc, it will return the content within, or with ACF checkboxes, which are stored as serialized data, $post->checkboxes will return a multi dimensional array.

    Can you think of a way to get ACF repeater fields returning the data and not just the row count?

  2. Thanks so much for this! This “magical” behavior actually led to a bug in my code. Any idea where I can learn more about this functionality? This is the only reference to it I can find on the entire internet…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.