0

i have a function to automatically add the first letter of each post title as a term on a taxonomy called "index". This is the code:

function save_index( $post_id ) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    $slugs   = array('product1', 'product2', 'product3');
    $letter = '';

    // only run for post types selected

    if ( isset( $_POST['post_type'] ) && ( !in_array($_POST['post_type'], $slugs) ) ) {
        return;
    }

    // Check user capabilities
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    $taxonomy = 'index'; // our custom taxonomy

    if ( isset( $_POST['post_type'] ) ) {

        // Get the title of the post
        $title = $_POST['post_title'];

        // Get the first letter of the title
        $letter = substr( $title, 0, 1 );

        // Set to 0-9 if it's a number
        if ( is_numeric( $letter ) ) {
            $letter = '0-9';
        }
    }

    // set term as first letter of post title, lower case
    wp_set_object_terms( $post_id, $letter, $taxonomy );
}

add_action( 'save_post', 'save_index' );

It's a rather simple code, however it's doing something unexpected: When i select a post on the dashboard and do bulk edit and update it, WordPress removes the index term.

And when i do "quick edit" and update it again, it adds it back (as expected).

But why is it removing the term when doing bulk edit?

Any help is appreciated.

2

Not sure it is the specific issue that causes it, but what you are doing wrong is to assume the existence of global variable, in this case $_POST['post_title']. Not everything that updates posts will have it set.

The right way to do what you are trying to do is to hook your hook on higher priority and get the relevant information from the wordpress DB itself by either using the second variable passed to the hook, or by doing a $post = get_post($post_id) and replace $_POST['post_title'] with $post->post_title

  • hi, i've made the modifications suggested but the problem remains. Bulk edit removes the term, quick edit adds it back. – Michael Rogers Mar 23 '18 at 22:12
  • OK working. you were right the title was 1 of the problems, i used $title = get_the_title( $post_id ); The second problem was if ( isset( $_POST['post_type'] ) ) { this line stopped it from working. What i don't understand is how it got to this point because i also have a if ( isset( $_POST['post_type'] ) ) return; check above but that one is not causing any issue. Oh well! At least it works. I mean if bulk edit doesn't use $_POST then why isn't it falling on the return? – Michael Rogers Mar 23 '18 at 22:34
  • 1
    you just can not know what will be there. Maybe the post_type is set at the context of bulk change, but it most likely will not be set when publishing new post from xml-rpc, json, or any of the import oriented plugins – Mark Kaplun Mar 23 '18 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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