How would I be able to insert data into the post meta table? I know how querying and wpdb works but I have no idea how I can just insert it into the table.

I have these two fields I want to insert ID and Title where Title should become a new post in my Custom Post type Company and ID should be the Content of it. I unfortunately have no idea how to do this.

EDIT: I’m not sure if it HAS to be the post meta table but as long as a new Custom post type post appears.

Thanks in advance!

2 Answers
2

You do not need to use the post_meta here, as all the Information is available in posts.

To insert a new post, use wp_insert_post( $post ), and pass the arguments to your $post-array. This function can return a WP_Error-object for Error handling (if the second argument is set to true, returns 0 on error if false), and returns the ID of the inserted post.

See the full list of arguments for wp_insert_post() on the Codex.

$post = array(
    'post_content'   => $content, // The content you want to have set in the content
    'post_title'     => $title, // The title of your post.
    'post_status'    => 'publish', // Whatever status you want to have
    'post_type'      => 'your_custom_post_type' // the slug of your custom post type
); 

$thisid = wp_insert_post( $post, true ); // insert the post and allow WP_Error object

if ( is_wp_error( $thisid ) ) {
    // Error handling
} else {
    // the rest of your code, inserting metadata
    update_post_meta( $thisid, 'your_meta_key', $your_meta_value );
}

Leave a Reply

Your email address will not be published. Required fields are marked *