How to create a new field in the post using "meta_input" (Wordpress)

Asked

Viewed 31 times

0

I’m trying to create a new field in all posts with post_type noo_company, using the meta_input, only that it’s not working. Not registering or saving the values of the field in the database, could anyone help me?

Below is an excerpt from the code:

$company_data = array(
    'post_title'     => $company_name,
    'post_type'      => 'noo_company',
    'comment_status' => 'closed',
    'post_status'    => 'publish',
    'post_author'    => $user_id,
    'meta_input'     => array(
         'cnpj' => $_POST['user_cnpj']
    )
);

$company_id = wp_insert_post( $company_data );

if ( ! empty( $postarr['meta_input'] ) ) {
    foreach ( $postarr['meta_input'] as $field => $value ) {
            update_post_meta( $post_ID, $field, $value );
    }
} 

if( !is_wp_error( $company_id ) ) {
    update_user_meta( $user_id, 'employer_company', $company_id );
    // Save custom fields
    jm_company_save_custom_fields( $company_id, $_POST );
}

Note: I am trying to create the "CNPJ" field. The values are retrieved from a form through the POST method.

  • The stack snippet (called code snippet) is obviously used to run javascript, html and css, ie front-end, there is no reason to use stacksnippet to put php, c++, java, c#, this will never work.

  • Work with the custom fields of Wordpress itself.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.