Custom Fields
Re-Envisioned
Support MB Frontend Submission Post Title & Name snippet changing Publish to Review

  • Creator
    Topic
  • #20999
    Resolved Nick
    Participant

    I have a CPT “Company.” It only uses custom fields, no title or content.

    Due to no title, I need my frontend submission form to create it from a field. I’m using the following code snippet:

    function iswp_update_company_title( $data )
    {
      if($data['post_type'] == 'company' ) { // Check post type
        $title = $_POST['company-name'];
        $slug = sanitize_title_with_dashes ($title,'','save');
        $slugsan = sanitize_title($slug);
        $data['post_title'] = $title ; //Updates the post title to your new title.
        $data['post_name'] = $slugsan; //Updates the post name aka slug to sanitized version of title.
      }
      return $data; // Returns the modified data.
    }
    add_filter( 'wp_insert_post_data' , 'iswp_update_company_title');

    This works fine on the front end submission. If I go into the admin area to update a company, it also seems to work fine.

    However, if I create a new Company from the admin area, the Publish button changes to Submit for Review and I see the warning “You are currently editing the page that shows your latest posts.”

    What do I have wrong?

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #21000
    Long Nguyen
    Moderator

    Hi,

    You need to check the field value before creating a new post in the admin area. If the field has no value at the first load and post title is set with an empty value, it happens the error WordPress database error:: [Column 'post_title' cannot be null].

    function iswp_update_company_title( $data ) {
      if( $data['post_type'] == 'company' ) { // Check post type
        if( isset( $_POST['company-name'] ) ) {
            $title = $_POST['company-name'];
            $slug = sanitize_title_with_dashes( $title,'','save' );
            $slugsan = sanitize_title( $slug );
            $data['post_title'] = $title ; //Updates the post title to your new title.
            $data['post_name'] = $slugsan; //Updates the post name aka slug to sanitized version of title.
        }
        
      }
      return $data; // Returns the modified data.
    }
    add_filter( 'wp_insert_post_data' , 'iswp_update_company_title');
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.