5. update_field() et delete_field() | Field Forge - Champs personnalisés, conçus pour la vitesse
Télécharger Se connecter

5. update_field() et delete_field()

Créer, mettre à jour ou supprimer des valeurs de champ par programme à partir du code PHP.

update_field( $selector, $value, $post_id )

Paramètres :
ParamètreTypePar défautDescription
$selectorstringNom du champ ou clé du champ
$valuemixedNouvelle valeur (le type dépend du type de champ)
$post_idint\string\falsefalseID de publication, 'options', 'user_N', ou false pour la publication actuelle
Renvoie : int — l’ID d’enregistrement de la valeur. Format de valeur par type de champ :
Type de champValeur attendue
text, textarea, email, url, passwordstring
number, rangestring ou int/float
image, fileID de pièce jointe (int)
select, radio, button_groupChaîne de valeur ou tableau si multiple
checkboxTableau de chaînes de valeur
true_falsetrue/false ou 1/0
relationship, post_objectID de publication ou tableau d’IDs de publication
taxonomyID de terme ou tableau d’IDs de terme
userID d’utilisateur ou tableau d’IDs d’utilisateur
date_pickerChaîne de format 'Ymd' (par exemple, '20241225')
color_pickerChaîne hexadécimale (par exemple, '#FF5733')
galleryTableau d’IDs de pièces jointes
repeaterTableau de tableaux de lignes
php
// Update a text field
update_field( 'subtitle', 'New Subtitle', 42 );

// Update an image field (pass attachment ID)
update_field( 'hero_image', 155, 42 );

// Update a select field
update_field( 'product_status', 'in_stock', 42 );

// Update a checkbox field (array of values)
update_field( 'amenities', [ 'wifi', 'parking', 'pool' ], 42 );

// Update an options page value
update_field( 'site_phone', '+1-555-0123', 'options' );

// Update a user field
update_field( 'biography', 'Senior developer with 10 years experience.', 'user_5' );

// Bulk update in a loop
$products = get_posts( [ 'post_type' => 'product', 'numberposts' => -1 ] );
foreach ( $products as $product ) {
    $current_price = get_field( 'price', $product->ID );
    update_field( 'price', $current_price * 1.1, $product->ID ); // 10% increase
}

// Import data from CSV
$csv = array_map( 'str_getcsv', file( '/path/to/products.csv' ) );
$headers = array_shift( $csv );
foreach ( $csv as $row ) {
    $data = array_combine( $headers, $row );
    $post_id = wp_insert_post( [
        'post_title'  => $data['name'],
        'post_type'   => 'product',
        'post_status' => 'publish',
    ] );
    update_field( 'price', $data['price'], $post_id );
    update_field( 'sku', $data['sku'], $post_id );
}

delete_field( $selector, $post_id )

Supprimer une valeur de champ de la base de données.

Paramètres :
ParamètreTypePar défautDescription
$selectorstringNom du champ
$post_idint\string\falsefalseID de publication, 'options', ou 'user_N'
Renvoie : booltrue en cas de succès.
php
// Delete a single field value
delete_field( 'old_field', 42 );

// Delete an option value
delete_field( 'deprecated_setting', 'options' );

// Delete a user field
delete_field( 'temp_code', 'user_5' );

// Clean up fields when a post transitions to trash
add_action( 'wp_trash_post', function( $post_id ) {
    delete_field( 'cached_summary', $post_id );
    delete_field( 'cached_thumbnail', $post_id );
});

// Remove all Field Forge values for a post
FIELDFORGE_Field_Values::instance()->delete_all( $post_id );

Assistant IA Forge En ligne

Bonjour ! Je suis l'assistant IA Field Forge. Posez-moi n'importe quelle question sur le plugin — configuration, fonctionnalités, dépannage ou développement.

À l'instant
Propulsé par Forge IA · Parcourir la documentation