2013-10-14 52 views
0

我做這個簡單的代碼中添加一個新的職位,以WordPress的DB(外循環):WordPress - 如何添加/更新post_meta(自定義字段)?

include_once './wp-config.php'; 
include_once './wp-load.php'; 
include_once './wp-includes/wp-db.php'; 

$upname = 'TestName'; 
$updescription = 'Description'; 

    // Create post object 
    $my_post = array(); 
    $my_post['post_title'] = $upname; 
    $my_post['post_content'] = $updescription; 
    $my_post['post_status'] = 'draft'; 
    $my_post['post_author'] = 1; 

    if (current_user_can('manage_options')) { 

    // Insert the post into the database 
    wp_insert_post($my_post); 
    echo '<br /><b>The Advertisement is saved as Draft!</b>'; 
    } else { 
    echo '<br /><b>You are NOT logged in, login to continue!</b>'; 
    } 

它的工作原理100%,但現在我要補充2自定義字段:「PHONE_NUM」「 birth_date「

如何在此代碼中添加自定義字段?

+0

檢查http://stackoverflow.com/questions/4901544/wp-insert-post-php-function-and-custom-fields –

回答

1

我找到了一個解決方案,這個工作對我來說:-)

在我的代碼我更新/更換:

// Insert the post into the database 
wp_insert_post($my_post); 

有了這個:

// Insert the post into the database 
$post_id = wp_insert_post($my_post); 
update_post_meta($post_id,'phone_num',$upphone); 
update_post_meta($post_id,'birth_date',$upbirthdate); 
0

你應該使用這個,它會幫助你更多。

我已經使用這個,它工作正常。

// Insert the post into the database 
$post_id = wp_insert_post($my_post); 
add_post_meta($post_id,'phone_num',$upphone); or 
    update_post_meta($post_id,'phone_num',$upphone); 
add_post_meta($post_id,'birth_date',$upbirthdate); or 
    update_post_meta($post_id,'birth_date',$upbirthdate); 

謝謝。

相關問題