0

I want to get the selected tags using publish_post hook, while publishing the post.

With below code, I can get only those tags which are already saved in the post. this will not work for new tags.

add_action( 'publish_post', 'post_published_notification', 10, 2 );
function post_published_notification( $ID, $post ) 
{
    $post_tags = get_the_tags($ID);
    if ( $post_tags ) 
    {
        print_r($post_tags);
    }
}

1 Answer 1

0

if you want to get all selected tag in "publish_post" try below code. it will give you selected tag from post. remove wp_die("all Tags"); after verify that you can get proper tags on publish post.

add_action( 'publish_post', 'post_published_notification', 10, 2 );
function post_published_notification( $ID, $post ) 
{
    $all_tags = $_POST['tax_input']['post_tag'];
    if ( $all_tags ) 
    {
        echo "<pre>";
        print_r($all_tags);
        echo "</pre>";
        wp_die("all Tags");
    }
}

It will output all selected tags by comma separated like :

new10,new4,new5,new6,new7,new8,new9

Let me know if this works for you!

4
  • There is nothing in $_POST Oct 15, 2019 at 6:24
  • i have tested code and it is working fine for me prnt.sc/pje76a. did you try with new post ? Oct 15, 2019 at 6:30
  • Oh.. I'm checking this on draft posts from start. Oct 15, 2019 at 6:33
  • it will also work with draft to publish post. did you try with changing post status from publish to draft then publish post? did you get data in $_POST ? Oct 15, 2019 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.