最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is proper filter or hook action to add data to the $post object?

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

According 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

发布评论

评论列表(0)

  1. 暂无评论