I have a custom post type that has a one to many relationship with another custom post type. Call them lesson and topics. Currently, I get $topics as

$args = array(
                'meta_key' => 'lesson_id',
                'meta_value' => $post['post']->ID,
                'post_type' => 'plugin-topic',
                'post_status' => 'published',
                'posts_per_page' => -1
            $les_topics = get_posts($args);

However, when the $lesson object is created by wordpress for first time, I would like to add $topics to it so that I can reference as $lesson->topics.

Can someone tell me the appropriate filter or action to use to accomplish that? I saw this

Adding additional data to WP_Post object

which suggests I shouldn't even do what I am asking to do but it isn't clear why this would be a bad thing.

thanks! Brian

1 Answer 1


According to https://developer.wordpress.org/reference/functions/wp_insert_post/ (looking at the source code) you could use wp_insert_post_data link and inject your relation, whenever a post is inserted.

$post is usually a global variable though when a page is loaded, which you can adjust using the_post link

  • Ok, that makes sense about the global $post object. The thing with wp_insert_post_data is that I just want to have this additional data upon the post retrieval before I display it, not upon post insertion tot he db. спасибо!
    – Brian
    Oct 27, 2019 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.