Support » Plugins » Hacks » Need help with insert_post and custom fields

  • I am using the following piece of code to bulk import data from a mysql database into the wp database

    $conn =	mysql_connect($dbhost,$dbuser,$dbpasswd);
    $results = mysql_query("SELECT * FROM data LIMIT 10",$conn);
    $i = 0;
    while ($row = mysql_fetch_array($results,MYSQL_ASSOC)) {
    $ID = $row['adid'];
    $Title = stripslashes($row['adtitle']);
    $Title = utf8_encode($Title);
      $post = array();
      $post['post_status'] = 'publish';
      $post['post_category'] = array(5,6);
      $post['post_title'] = $Title;
      $post['post_content'] = $row['subcatname'];
      $post['tags_input'] = $Tags;
      $posts[$i] = $post;
    foreach ($posts as $post) {
    echo $Tags;

    Everything works fine so far but i need to import some custom fields also and i dont know how to add them into this code. Anybody can help ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • If you’re wanting to add additional fields to a post you can use add_post_meta() (

    It looks like it is a part of wp-includes/post.php so you should have access to the method if you have access to wp_insert_post.

    Hope that hit what you were looking for.

    Thank you, that points me in the right direction. Now i modified the last part:

    foreach ($posts as $post) {
    $published_id = wp_insert_post($post);
    add_post_meta($published_id, "cp_extern", $Title);

    and it imported one custom field of the last post. But where do i have to put the code so that it imports the custom fields of all 10 posts ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Need help with insert_post and custom fields’ is closed to new replies.