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 );
  • So this generates everything in the wp_posts table? For example the link etc? Although i'm not sure how i'm going to accomplish the following, say I have queried data from a secondary non WP database(which I have) and say i'd want that data to go into the WP DB. Would I need to add a button on the bottom of it and make an isset that would execute what you said? If so than this is exactly what I need.
    – David H
    Mar 13, 2014 at 10:33
  • Also one last thing post_content say the content is a field made with ACF how would I instead call that?
    – David H
    Mar 13, 2014 at 10:45

You can execute any query as follows and check for success , Let me know if it works.


$InsertQuery = "INSERT INTO post_meta VALUES (Enter Values Here)";

//Create a query named InsertQuery

$insert = $wpdb->query($InsertQuery);

//Execute InsertQuery

    echo 'Post Entered Successfully.';
    echo 'Unable to Insert Post.';

//Check if the Query has run successfully
  • Yes I know this is possible but when I look at the database I notice a few things. What I want is to insert a bit of data into the columns post_title and post_content. BUT I want it to go to a custom post type called Company which means in my VALUES I also have to enter post_type. But what I am wondering is if it's okay to just enter those values and being able to edit them in WP_Admin without having filled in for example the column guid .
    – David H
    Mar 13, 2014 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.