WordPress wp_update_post() inserts empty post_title

I am using the WordPress theme WPJobus and I had the necessity to modify the workflow of Resume Submission and Resume Editing.

The functions which provide for these operations are located as usual into the file /mytheme/functions.php

Read More
wpjobusSubmitResumeForm()
wpjobusEditResumeForm()

On both of them I have removed the whole code portion which switch post status to “Draft” on every edit occurrence, since I needed to give to the new user the possibility to insert their own posts (Resumes) and publish without the Administrator supervision.

Anyway, I made the all changes and everything works fine except the function wp_update_post() with posts.post_title field!

The code looks like this:

$post_information = array(
    'ID' => $td_current_post,
    'post_title' => $_POST['fullName'],
    'post_content' => strip_tags($_POST['postContent']),
    'post_type' => 'resume',
    'post_name' => $td_current_post,
    'comment_status' => 'closed',
    'ping_status' => 'closed',
    'post_status' => 'publish'
);

$td_post_id = wp_insert_post($post_information);

wp_update_post( 
            array(
                'ID' => $td_post_id,
                'post_name' => $td_post_id 
            ) 
);

The INSERT works fine but when I call the UPDATE, which is absolutely necessary for post_name field, post_name is correctly updated while post_title value is deleted.

I got the same effect even if I try to pass ‘post_title’=>$_POST[‘fullName’] within wp_post_update array.

Anyone had the same issue?

I also found this but it didn’t help at all:
wp_update_post inserts empty title and content

Related posts

2 comments

  1. Thank you for your answer doublesharp!

    I finally found the origin of the problem just reading carefully the functions.php file row by row.

    The function which was causing the problem is filter_handler() on line 636.

    In this case for me it was enough to comment the hook add_filter() related to this function and I achieved the goal.

    add_filter( 'wp_insert_post_data' , 'filter_handler' , '99', 2 );
    

    Now, if I run the script above, wp_post_update() works like a charm with Resume Post Type ( I guess with Company and Job Post Types as well ).

    I will make some more tests and will update this thread in case I would face some unexpected issue.

    Here the code of the function:

    function filter_handler( $data , $postarr ){
    global $post, $id;
    
    if(('job' == $data['post_type'] && isset($data['post_type'])) or ('company' == $data['post_type'] && isset($data['post_type'])) or ('resume' == $data['post_type'] && isset($data['post_type']))) {
    
        $id = $postarr['ID'];
    
        if($id) {
    
            $title = $_POST['ID'];
            $wpjobus_title = esc_attr(get_post_meta($title, 'wpjobus_post_title',true));
    
            if(!empty($wpjobus_title)) {
                $data['post_title'] = $wpjobus_title;
            } else {
                $data['post_title'] = $title;
            }
    
            $data['post_name'] = $title;
        }
    
    }
    
    return $data; }
    

    Another solution should be to force the call of filter_handler() Only if the current user is Administrator using the related ‘controller’:

    if (current_user_can('administrator')) {
    

    For sure the problem is located within that function so any possible correction must be done on it.

  2. Now that you have provided more information, the correct way to change this behavior is to remove the filter that is being added by your theme, which will give you control of the post_title.

    // add an action after the theme has loaded - wp_loaded is safe
    add_action( 'wp_loaded', function(){
        // check for permissions?
        // if ( !current_user_can( 'administrator' ) ) return;
    
        // remove the filter that the theme added
        remove_filter( 'wp_insert_post_data' , 'filter_handler' , '99', 2 );
    } );
    

    Without access to the source code for your theme it’s not possible to say where the error is coming from, however you should be able to use the pre_update_posts action to debug, and possibly correct the behavior using the wp_insert_post_data filter.

    To use the pre_update_posts for debugging, you can print the contents of the $data array that is used to update the posts table, and the $postarr array which contains the original fields that you passed in (it can’t be modified using filters).

    // print the $data and $postarr to the error log when inserting/updating
    add_action( 'pre_update_posts', function( $data, $postarr ){
        error_log( 'The $data array contains ' . print_r( $data, true ) );
        error_log( 'The $postarr array contains ' . print_r( $postarr, true ) );
    }, 100, 2 );
    

    The last available place to modify post data that is being inserted/updated is in the wp_insert_post_data filter. By attaching to this filter later than everything else, you have the final say in the array values.

    add_filter( 'wp_insert_post_data', function( $data, $postarr ){
        // you could also log here for debugging
        // error_log( 'The $data array contains ' . print_r( $data, true ) );
        // error_log( 'The $postarr array contains ' . print_r( $postarr, true ) );
    
        // set the title to whatever was initially passed in
        $data['post_title'] = $postarr['post_title'];
    
        // return the modified data so that it can be used in the update
        return $data;
    }, 999, 2 );
    

Comments are closed.