I’m trying to use the function get_the_tags()
from outside the ‘loop’.
I understand this can be achieved by using the post ID like get_the_tags($postID)
. Does anyone know how I can get the post ID from inside a wp_insert_post_data
function?
I’ve tried using 'guid'
which is suggested here, although I’ve had no luck. I’m also not sure that’s even the post ID. Any help with this will be appriciated. Thanks.
EDIT:
Here’s the code I’m working with:
function changePost($data, $postarr) {
$postid = $postarr["ID"];
$posttags = $postarr['tags_input']; // This doesn't work.
$content = $data['post_content'];
$subject = $data['post_title'];
if($data['post_status'] == 'publish') {
sendviaemail($content, $subject, $postid, $posttags);
}
return $data;
}
add_filter('wp_insert_post_data','changePost','99',2);
As you can see, I want to send the post ID, post tags, content and the subject to another function called sendviaemail
. Everything is okay, except I don’t know how to get the tags from the post.
In the following, ’10’ is the priority that
my_func
gets called and ‘2’ is the number of arguments thatmy_func
accepts. The latter is important, since theadd_filter
function defines the default as 1, but thewp_insert_post_data
filter hook sends two arguments. If you don’t set this as 2 you won’t get the second argument.Now make your function…
EDIT— based on your added code above
If you don’t need to modify the post’s
$data
before the post is saved then you’re using the wrong hook.Use the
save_post
action hook instead. This gets called after the post is saved and all the taxonomies are saved. So you don’t have to worry about whether new tags have been added. It sends two arguments to your function: the ID of the post and the post itself as an object.