1

I have this function to set the title of a wordpress post which works properly for saving / updating posts. The major issue is that the function stops posts being deleted (it removes the title but the post still exists).

Is there a way to identify if the action is a delete post rather than save / update post and obviously i don't return the $data if a delete action?

add_filter( 'wp_insert_post_data', array($this, 'updatetitle' ) , '99', 1 ); 

public function updatetitle($data) {

    if($data['post_type'] != $property->post_name || !$_POST[${$this->active_class_var_name}->noncefield] ):
        return $data;
    endif;  

    if($_POST['address'] && $_POST[$property->noncefield]):
        $data['post_title']= ucfirst (sanitize_text_field($_POST['address'])).' '.ucfirst (sanitize_text_field($_POST['town'])).' '.ucfirst (sanitize_text_field($_POST['county']) );
    endif;

    return $data;
}
1

Not sure, but seems maybe $data['post_status'] would be set to 'trash' at that point. If so, then you could just do something like...

if ($data['post_status'] == 'trash') { return $data; }

...before the other manipulations.

EDIT

That code should work then - but needs to be at top of the function, like...

public function updatetitle($data) {

    if ($data['post_status'] == 'trash') { return $data; }

    if($data['post_type'] != $property->post_name || !$_POST[${$this->active_class_var_name}->noncefield] ):
        return $data;
    endif;  

    if($_POST['address'] && $_POST[$property->noncefield]):
        $data['post_title']= ucfirst(sanitize_text_field($_POST['address'])).' '.
            ucfirst (sanitize_text_field($_POST['town'])).' '.
            ucfirst (sanitize_text_field($_POST['county']) );
    endif;

    return $data;
}

Your function in that case would simply return back the $data without making any other modifications to it.

1
  • the post status == trash evals to true but it still is the same, the title is removed but the post still exists :(
    – David
    Oct 11 '14 at 17:27
0

Try the save_post hook instead of wp_insert_post_data.

It should solve your problem

1
  • Im not sure you can modify the post data from there?
    – David
    Oct 11 '14 at 20:37

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.