$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Unable to update the meta boxes with multiple fields|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Unable to update the meta boxes with multiple fields

matteradmin9PV0评论

I successfully build a function, that is able to import data from a CSV into posts of a Custom Post Type (CPT). I am able to parse data into the native Title and Description fields, but it fails, when I parse data into a meta box with multiple fields.

Here is a code snippet of the function that are parsing CSV data into post fields:

// Check and see if the current post exists within the database.
$check_post_exists = function( $title ) use ( $wpdb, $postTypeArray ) {

    // Get an array of all posts within the custom post type
    $posts = $wpdb->get_col( "SELECT post_title FROM {$wpdb->posts} WHERE post_type = '{$postTypeArray["custom-post-type"]}' AND post_status = 'publish'" );

    // Check if the passed title exists in array
    return in_array( $title, $posts );
}

$i = 0;

foreach ( $posts() as $post ) {

    // If the post exists, skip this post and go to the next one
    if ( $check_post_exists( $post["zoneid"] ) ) {
        continue;
    }

    $i++;

    // Insert the post into the database
    $post["id"] = wp_insert_post( array(
        "post_title" => $post["zoneid"],
        "post_content" => $post["bemaerkning"],
        "post_type" => $postTypeArray["custom-post-type"],
        "post_punktcat" => array( 4 ),
        "post_status" => "publish"
    ));

    // Set post category to the value "4"
    wp_set_post_terms($post["id"], 4, 'punktcat', false );

    // Parse data into a custom field normally referred 
    // THIS IS UNSUCCESSFUL
    update_post_meta($post["id"], $fredningszone_data['id'], $post["zoneid"]);
}

echo '<div id="message" class="updated fade"><p>' . $i . ' posts have succesfully been imported from CSV!' . '</p></div>';

So the update_post_meta() function in the code snippet above does not provide the data of the zoneid into the custom field.

The way I build my custom field and be seen within the following code snippet:

function fredningszone_meta_box() {

    add_meta_box( 
        'punkt_fredningszone', // $id - Meta box ID (used in the 'id' attribute for the meta box).
        'Data for fredningszone', // $title - Title of the meta box.
        'punkt_fredningszone_callback', // $callback - Function that fills the box with the desired content. The function should echo its output. 
        'punkt', // $screen - he screen or screens on which to show the box (such as a post type, 'link', or 'comment'). 
        'normal', // $context - The context within the screen where the boxes should display.
        'high' // $priority - The priority within the context where the boxes should show ('high', 'low').
    );
}

add_action( 'add_meta_boxes', 'fredningszone_meta_box' );

/**
 * Enable and display custom fields.
**/

function punkt_fredningszone_callback() { 

    global $post;  

    $meta = get_post_meta( $post->ID, 'fredningszone_data', true ); ?>

    <input type="hidden" name="fredningszone_data_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">

    <!-- All fields goes below this line -->

    <p>
        <label for="fredningszone_data[id]">ID</label>
        <br>
        <input type="text" name="fredningszone_data[id]" id="fredningszone_data[id]" class="regular-text widefat" placeholder="Indtast fredningszonens ID (f.eks 450)" value="<?php echo $meta['id']; ?>">
    </p>

<?php }

I will appreciate, if anyone would have a suggestion on how I could parse the data into one of the fields of my custom metabox with multiple fields.

Post a comment

comment list (0)

  1. No comments so far