Ready to get started?Download WordPress


save_post & post_status help (4 posts)

  1. jspitz725
    Posted 1 year ago #


    I'm trying to get the post_status of a post before it is updated in the database. If I hook into save_post, I'm hoping that the post_status will have already been changed to publish. Am I correct?

    Essentially, I have this hack where I modify the post content, but only when a post is published or updated. I don't want to modify the post content if the post is a draft or a preview or anything of that sort.

    How can I check this on the fly? I've tried get_post_status(''), but let's say I'm working on a draft...get_post_status('') == 'draft' and when I click publish, my code still considers it as a draft until the page gets refreshed (then it considers it as published).

    Sorry for the confusing question and thank you in advance for any help. I can clarify any questions.


  2. bcworkz
    Posted 1 year ago #

    'save_post' fires for any number of statuses, you cannot assume it has been set to publish. However, it does fire after whatever status has been saved to the DB.

    If you want a action that only fires for posts that have their status set to publish after an update, use 'publish_post'. Check the source code for wp_transition_post_status() in wp-includes/post.php for several other variants of actions that fire for different status transitions, all just after being saved to the DB.

  3. jspitz725
    Posted 1 year ago #

    Thank you. After looking at wp_transition_post_status() I noticed that save_post fires AFTER the post status is set to publish. I was pretty confused before, but now save_post works.

    The only question I have left is what the hell the difference is between $_POST["content"] and $_POST["post_content"]

    Thanks again!

  4. bcworkz
    Posted 1 year ago #

    $_POST["content"] is a PHP array element containing the post content submitted when you click "Update". This content is in a textarea form element with the attribute name="content".

    There should not be a $_POST["post_content"] because there is no form element with that name. Where 'post_content' comes from is the column name in the WP posts DB table. As such, when you use a function such as get_post(), 'post_content' is a key name of the post array returned by the function. So if your code was:
    $mypost = get_post(227, ARRAY_A);

    $mypost will be assigned an associative array for post ID 227 (because of the ARRAY_A parameter, otherwise by default you get an object). Thus you can access the post's content with $mypost["post_content"]. But the super global $_POST would typically never have an array key "post_content" unless some theme or plugin modified an edit form.

Topic Closed

This topic has been closed to new replies.

About this Topic