I want to add additional items to the data returned as WP_Post. So for every function/query that returns a WP_Post object I want my additional data added.
Example of returned result:
WP_Post (object) => [ // Default data returned by WP_Post ID => int, post_author => string, post_name => string, post_type => string, post_title => string, post_date => string, post_date_gmt => string, post_content => string, post_excerpt => string, post_status => string, comment_status => string, ping_status => string, post_password => string, post_parent => int, post_modified => string, post_modified_gmt => string, comment_count => string, menu_order => string, // Additional data I want to add extra_data_1 => array, more_data_key => string, another_added => string ] For example when the functions get_post() or get_page_by_path() are run they will return the WP_Post object along with my additional data.
I've tried finding the appropriate hook/filter but this has been unsuccessful.
I am hoping I can do something like:
// This is concept code add_action('pre_wp_post_return', function($data) { $data->extra_data_1 = get_post_meta($data->ID, 'extra_data'); $data->more_data_key = get_post_meta($data->ID, 'more_data', true); $data->another_added = get_post_meta($data->ID, 'another_data', true); return $data; }); My reasoning is I am having to build a custom API for WP that uses a range of different core functions which return WP_Post objects. Being able to add my additional data in one place would prevent me from duplicating code.
I hope this is clear enough. Any help would be great!
post_meta?WP_Postobject does not include this by default. I have updated my question to stop confusion. My reasoning is I am having to build a custom API for WP that uses a range of different core functions which returnWP_Postobjects. Being able to add my additional data in one place would prevent me from duplicating code.