简体   繁体   English

高级自定义字段和前端过帐

[英]Advanced Custom Fields and frontend posting

I have created a frontend form that crates a new custom post type single. 我创建了一个前端表单,用于创建新的自定义帖子类型单。 Is there anyway to update the ACF meta from this form? 无论如何,有没有从此表格更新ACF元数据?

<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {


    // Add the content of the form to $post as an array
    $post = array(
        'post_title'    => $description,
        'post_content'  => $description,
        'post_category' => 3,  // Usable for custom taxonomies too
        'post_status'   => 'private',           // Choose: publish, preview, future, etc.
        'post_type' => 'door', // Use a custom post type if you want to
        'post_parent' => $post->ID,
        'post_name' => 'service',
        'meta_key'      => 'archive_job',
        'meta_value'    => 'red',
    );
    wp_set_object_terms($pid, $_POST['terms'], 'child');
    wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function
    global $wp;
    $current_url = home_url(add_query_arg(array(),$wp->request));
    wp_redirect( $current_url );

                    // http://codex.wordpress.org/Function_Reference/wp_insert_pos
} // end IF

// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post', 'add_post_meta' , 'update_post_meta'); 

?>

ACF meta are saved in database as basic custom field . ACF元数据作为基本自定义字段保存在数据库中。 So you can use for example function add_post_meta() with values add_post_meta($post_id, 'field_name', $field_value); 因此,您可以使用例如函数add_post_meta()和值add_post_meta($ post_id,'field_name',$ field_value); after wp_insert_post($post); 在wp_insert_post($ post);之后 function. 功能。

example code: 示例代码:

    $post = array(
        'post_title'    => $description,
        'post_content'  => $description,
        'post_category' => 3,  // Usable for custom taxonomies too
        'post_status'   => 'private',           // Choose: publish, preview, future, etc.
        'post_type' => 'door', // Use a custom post type if you want to
        'post_parent' => $post->ID,
        'post_name' => 'service',
        'meta_key'      => 'archive_job',
        'meta_value'    => 'red',
    );
    wp_set_object_terms($pid, $_POST['terms'], 'child');
    $post_id = wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function
    add_post_meta($post_id, 'field_name', $field_value);
    global $wp;
    $current_url = home_url(add_query_arg(array(),$wp->request));
    wp_redirect( $current_url );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM