• Resolved warchief626

    (@warchief626)


    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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');
    ?>

    Thread Starter warchief626

    (@warchief626)

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

    Thread Starter warchief626

    (@warchief626)

    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!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Do action on publish?’ is closed to new replies.