2013-10-14 5 views
0

J'ai fait ce code simple d'ajouter un nouveau poste à WordPress DB (hors LOOP):WordPress - Comment ajouter/mettre à jour post_meta (Custom Fields)?

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>'; 
    } 

Il fonctionne à 100%, mais maintenant je souhaite ajouter deux champs personnalisés: "phone_num" et " date_date "

Comment puis-je ajouter des champs personnalisés dans ce code?

+0

vérifier http://stackoverflow.com/questions/4901544/wp-insert-post-php-function-and-custom-fields –

Répondre

1

J'ai trouvé une solution et cela a fonctionné pour moi :-)

Dans mon code, je mis à jour/remplacé:

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

Avec ceci:

// 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

Vous devrait utiliser cela, il vous aidera plus.

J'ai utilisé celui-ci, et cela fonctionne très bien.

// 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); 

Merci.