WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] publish_post without trigger on edit (8 posts)

  1. alexmat
    Member
    Posted 6 years ago #

    Is there any action like publish_post that does not trigger when a published post is edited?

    I send out an e-mail whenever a post is published, but don't want it sending e-mails when a post that was published is edited.

    Thanks,
    Alex

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Yep. I think it would be draft_to_publish, but you may want to test that theory. It might be new_to_publish instead.

    http://codex.wordpress.org/Post_Status_Transitions

  3. alexmat
    Member
    Posted 6 years ago #

    Thank you so much. "new_to_publish" works like a charm. Although it wasn't documented in the link you pointed me to, but that was also useful.

    Cheers!

  4. jeckman
    Member
    Posted 5 years ago #

    Does new_to_publish happen when scheduled posts get published as well?

    I'm looking for an action that is called whenever a post which was not previously published becomes published, but not when an existing post is edited.

  5. ajaswa
    Member
    Posted 5 years ago #

    @jeckman no you'll probably want something like this:

    add_action('future_to_publish', 'function_name');

  6. johneckman
    Member
    Posted 5 years ago #

    Thanks @ajaswa - but does future_to_publish also catch posts that were not scheduled but published immediately?

    Sorry if I'm missing something here - but the Post Status Transitions docs aren't clear to me.

    Would

    add_action('future_to_publish','function_name');
    add_action('new_to_publish','function_name');

    together cover all these cases:

    • New post, write, publish immediately
    • New post, save a draft, edit draft, publish
    • New post, schedule for publishing, publish when time arrives, regardless of whether it has been saved as draft and edited or not

    Might be easier just to use:

    add_action('publish_post','function_name');

    Since the "false positives" of having it occur when posts are edited is probably minimal impact as opposed to potentially missing a post.

  7. ajaswa
    Member
    Posted 5 years ago #

    @johneckman

    You are right the docs really suck on this subject. I've only figured this out by trial and error.

    To cover all 3 of your cases you'll probably want:

    add_action('future_to_publish','function_name');
    add_action('new_to_publish','function_name');
    add_action('draft_to_publish','function_name');

    Something else about this method is from what I can tell it applies to both posts and pages.
    I've found publish_post to be more annoying then helpful. There really ought to be a hook for just new publishes and one for edits.

  8. pcunix
    Member
    Posted 5 years ago #

    When you use add_action('publish_post','function_name');

    does function_name run BEFORE the post is actually published, sometime during the various actions, or after it is all complete and done?

Topic Closed

This topic has been closed to new replies.

About this Topic