Your code is interfering with the internal structure of WP_Query
and WP_Post
, and making assumptions about how it works. This is extremely unusual, and not best practice when developing with WordPress.
Case in point, officially there is no post_meta
member variable on that class ( https://developer.wordpress.org/reference/classes/wp_post/ ). That’s not how you fetch a posts meta key/values.
Instead, use a standard post loop, and standard API calls.
Here’s what a standard WP_Query
post loop should look like:
$args = [
// parameters go here
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// display the post
the_title();
the_content();
}
wp_reset_postdata();
} else {
echo "no posts were found";
}
Note that a standard post loop lifecycle happens, as well as all the expected hooks. You can do work inside the while
loop for each post. For example, fetching all the post meta. To do that, use the get_post_meta
function:
$all_meta = get_post_meta( get_the_ID() );
It might be tempting to think that a WP_Post
object follows the naive OO dream of an all encompassing API for posts, a one-stop representation of a single post and all the things that there is to know and can be done about it. But in reality it’s a data container. You won’t find class methods or methods of sub-classing it.
Additionally, by poking around and directly accessing the internal data structures, you bypass a lot of functionality and risk your code being broken in the future. For example post meta gets fetched in advance and stored in WP_Cache
to avoid query duplication. There are also filters that allow plugins and other code opportuniities to make changes or fix things.