WordPress.org

Ready to get started?Download WordPress

Forums

When Post is published assign Id? (4 posts)

  1. Dude_its_wordpress
    Member
    Posted 1 year ago #

    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?

  2. bcworkz
    Member
    Posted 1 year ago #

    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'.

  3. Dude_its_wordpress
    Member
    Posted 1 year ago #

    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.

  4. bcworkz
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.