WordPress dirty little save_post secrets
FYI => The WordPress action save_post isn’t quite what the WP codex makes it out to be. Or at least that’s my current perception. Let me explain…
The first thing worth noting is that the WordPress action save_post fires more than once. That is, the function you call with add_action(‘save_post’, ‘my_function’) is going to fire more than a single time. Yup. Fo’ real. So if you’re using save_post and expecting something to happen once and only once (as I originally expected in this case) you’ll have to add a snippet of to stop after the first cycle. I’ve used save_post before and for some reason this quirk was never an issue, except this time.
OK, not so bad. I found someone else’s snippet and tuned it up for the WP ezClasses library. No biggie.
But here’s where things get a bit too wonky for my tastes. The Action Reference page in the codex specifically says:
“Runs after the data is saved to the database.”
Best I can tell, that’s not true. It fires once before the DB save and then again after. The point being, the snippet I have in place to stop after one save cycle is not seeing the updated post data. Right. Exactly. WTF?!?!
I’ll update the save once snippet to skip Save #1 and use Save #2 but I’m a bit bothered by how something so obvious and important has gone unrevised in the WP codex.
Perhaps it’s just me. Is it just me?