You’ve added incorrectly for one thing:
add_action("rest_insert_post", "action_rest_insert_post", 10, 3);
(last arg should be 3, not 1)
If you checked the error log you should have seen a warning about invalid parameter count.
Thread Starter
cudjex
(@cudjex)
Actually I have tried with 10, 3 params too. Also I tried with my custom post types, it didnt work again.. Interesting really. I am adding dd(), var_dump() etc. to callback function but there is no error again.
Any other issues you encounter would be around $request properties. When I replace $params->ib_landing_params with a simple string it gets saved, so everything else is working. Verify get_json_params() is returning what you think it should be. Is it really an object and not an array?
Thread Starter
cudjex
(@cudjex)
I think the issue is not related with $request or $params etc. Because this is not working too:
function action_rest_insert_post( $post, $request, $update ) {
update_post_meta($post->ID, 'ib_landing_params', 'test');
}
add_action("rest_insert_post", "action_rest_insert_post", 10, 3);
I tried all ways and gave up lol 🙂
-
This reply was modified 4 years, 3 months ago by
cudjex.
You give up too easy 😛 Granted, I don’t know how much effort you’ve expended. FWIW, that works in my testing, so something else is amiss with the hook. Maybe the add_action() call occurs too late or under the wrong conditions? I put your code in a test plugin I use, so the action was added quite early on every request of any sort.
There are also a number of other actions that fire when a post is saved. Most are not REST specific, but you can check for the constant REST_REQUEST being defined from a hook like “save_post”. Using a different hook may or may not help any.
Are you sure the post is saved via a REST API request and not through other means like Ajax? “save_post” fires for virtually any post insertion method.
Anyway, instead of giving up, set the problem aside for a while, then revisit later. A fresh look can reveal things you’re missing right now.