$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'); ?>functions - update_post_meta for custom field not working upon form submission|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)

functions - update_post_meta for custom field not working upon form submission

matteradmin9PV0评论

I have a custom field called _my_description

The idea is when a form is submitted the data from the form is also put into this field.

Here's my code

add_action( 'gform_post_submission_10', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {

    $post = get_post( $entry['post_id'] );

    $post-> _my_description = $entry[1] . ', ' . $entry[11] . ', ' . $entry[14] . ', ' . $entry[13] . ', ' . $entry[12] . ', ' . $entry[16] . ', ' . $entry[15] . $entry[19];

    update_post_meta( $post );
}

If I have it go to the post_content with wp_update_post it works fine, but trying to get it to go to a custom field it wont work. The fields were created using this plugin / Anyone see why this isn't working please?

Fixed Thanks If anyone needs the code in future here it is

add_action( 'gform_post_submission_10', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {

    $post = get_post( $entry['post_id'] );


update_post_meta( $post->ID, '_my_description', $entry[1] . ', ' . $entry[11] . ', ' . $entry[14] . ', ' . $entry[13] . ', ' . $entry[12] . ', ' . $entry[16] . ', ' . $entry[15] . $entry[19] ); 
}

I have a custom field called _my_description

The idea is when a form is submitted the data from the form is also put into this field.

Here's my code

add_action( 'gform_post_submission_10', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {

    $post = get_post( $entry['post_id'] );

    $post-> _my_description = $entry[1] . ', ' . $entry[11] . ', ' . $entry[14] . ', ' . $entry[13] . ', ' . $entry[12] . ', ' . $entry[16] . ', ' . $entry[15] . $entry[19];

    update_post_meta( $post );
}

If I have it go to the post_content with wp_update_post it works fine, but trying to get it to go to a custom field it wont work. The fields were created using this plugin http://justcustomfields/ Anyone see why this isn't working please?

Fixed Thanks If anyone needs the code in future here it is

add_action( 'gform_post_submission_10', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {

    $post = get_post( $entry['post_id'] );


update_post_meta( $post->ID, '_my_description', $entry[1] . ', ' . $entry[11] . ', ' . $entry[14] . ', ' . $entry[13] . ', ' . $entry[12] . ', ' . $entry[16] . ', ' . $entry[15] . $entry[19] ); 
}
Share Improve this question edited Feb 3, 2018 at 19:12 Chazlie asked Feb 3, 2018 at 18:54 ChazlieChazlie 351 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

The function signature for update_post_meta() looks as follows:

update_post_meta( int $post_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' )

So instead of

update_post_meta( $post );

you need to use

update_post_meta( $post->ID, 'my_post_meta_key', $content_you_want_to_add );

I can only assume that you want to add this description to post meta. For that you'd use

update_post_meta( $post->ID, '_my_post_description', $entry[1] . ', ' ...
Post a comment

comment list (0)

  1. No comments so far