Support » Plugins » Hacks » When Post is published assign Id?

  • I’m trying to use <?php get_the_ID(); ?> to the post just created and assign it to a variable.

    So it would be

    When Post is published assign Id and then assign it to a variable.

    Any ideas?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    To access the ID of a post just created, you could hook the action ‘wp_post_insert’. Unfortunately, this also fires on post updates. If you need a truly new post, you will want the action “{$old_status}_to_{$new_status}”, I’m not sure of the correct values for the variables though. You can determine them by investigating the values passed to the action ‘transition_post_status’.

    Is there a just published type status I could use in a loop?

    I’m trying to develop a plugin the function of the plugin is to register a post type, then for every time the specific post type is posted to
    automatically created child pages for the registered post type. Example posted Example automatically example/child was
    created, example/child2, example/child2/child, and example/child3. I have some of it figured out im just trying to figure out how to attach when post type created to create child pages.

    Moderator bcworkz


    Of course there is $post->post_status == ‘publish’ combined with a recent $post->post_date. I suppose you could create a scheduled task to find any published post that is newer than a certain time that is longer than the task interval. But tasks need to be triggered by a visit, so you need a decent traffic level for this to work right.

    I still think the old-new status variable action is more efficient. IIRC, the resolved action value you actually want is ‘draft_to_publish’, but no guarantees on that. Of course, either way, you would need to except your child creations because they trigger the same function, possibly causing infinite nesting.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘When Post is published assign Id?’ is closed to new replies.