Forums

[resolved] Do action on publish? (4 posts)

  1. Warchief626
    Member
    Posted 2 years ago #

    I'm editing wp-includes/post.php with a code bit that I only want done once the post is published. But because of how the wp_insert_post function works, it runs that code bit every time a draft is auto-saved as well.

    I need to know how I could run code ONLY upon the post being published, like in an "if($status == "published)" statement. ($post->post_status doesn't work as it retains draft state in the variable after being published for some reason).

    Thanks.

  2. Eric Mann
    Member
    Posted 2 years ago #

    The action 'publish_post' is run whenever a post is published or when the status becomes "published." It shouldn't fire when drafts are saved.

    So just:

    <?php
    add_action('publish_post', 'my_custom_function');
    ?>
  3. Warchief626
    Member
    Posted 2 years ago #

    I need to pass some variables to my function, and add_action seems confusing in that area. Can you help me out?

    There are 2 variables that need to be passed, both using data from the post (from the get_post function).

  4. Warchief626
    Member
    Posted 2 years ago #

    Ah never mind, I just edited the function a bit to be self dependent using nothing but $post_ID as an input and it worked.

    Thanks for your help!

Topic Closed

This topic has been closed to new replies.

About this Topic