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
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
Share Improve this question asked Oct 27, 2019 at 9:08 BrianBrian 3372 silver badges11 bronze badges1 Answer
Reset to default 0According to https://developer.wordpress/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