Save a custom field reading value from another

This topic contains 18 reply and 4 voices, and was last updated by Tareq Hasan 10 years, 11 months ago
Viewing 15 Posts - 1 through 15 (of 18 total)
Author Posts
December 4, 2013 at 6:21 am 13248
Tareq Hasan Hi. I have a field as POST TITLE. I'd like to save another hidden field with same data entered in POST TITLE field, without action by the user. When user is publish its post, simply reading POST TITLE value and save the same value with hidden meta key. Is this possible?
December 4, 2013 at 6:23 am 13249
Mahi Mahi

Very much possible 🙂

Check action hook tutorial: http://docs.wedevs.com/using-action-hook-field/

December 4, 2013 at 6:27 am 13250
Danilo Danilo

I saw this doc before, but I can’t use it.
You missed some information just like:
– In which files I need to put the code?

December 4, 2013 at 12:03 pm 13264
Tareq Hasan Tareq Hasan

You can put this code in your themes functions.php.

December 4, 2013 at 7:13 pm 13292
Danilo Danilo

First of all, thank you for support.

But, in this tutorial, you explain basic usage. How can I read value from another field of the same form?

Imagine that I put another field with date and I need to save another field with same entered date but with another format.
Could you extend the tutorial with some examples?

December 4, 2013 at 7:51 pm 13297
Tareq Hasan Tareq Hasan

Here’s another example of reading a value from another field and updating another 🙂

As you are getting post id, you can get post title based on the post id and update to another meta_key.

December 4, 2013 at 8:37 pm 13305
Danilo Danilo

Ok, it seems difficult but I’ll try today.

How can I do the same with a date field, changing date format in a second field?

December 4, 2013 at 9:00 pm 13307
Tareq Hasan Tareq Hasan

The link above exactly gives an example how to do that!

December 4, 2013 at 9:58 pm 13309
Danilo Danilo

The snippet you send me is about Woocommerce product, so code is displaying _price meta.

I can understand the example, but I need more information about how to retrieve another meta data, just like post title, and a lot of no-programmer user need this too…

I can’t find much information in tutorial.

December 5, 2013 at 12:47 am 13324
Denis Denis
function hereTheNameOfYourFunchtion( $post_id ) {
    $titleTheUserPutsIn = get_post_meta( $post_id, 'post_title', true );
 
    update_post_meta( $post_id, 'yourCustomField', $titleTheUserPutsIn );
}
 
add_action( 'wpuf_add_post_after_insert', 'hereTheNameOfYourFunchtion' );
add_action( 'wpuf_edit_post_after_update', 'hereTheNameOfYourFunchtion' );

I think this will work.

December 5, 2013 at 2:21 am 13326
Danilo Danilo

DenisCGN, I put your function but I get a blank key.
My custom field is created, but is blank.

I’m using the function with a hidden input field. Maybe this is the error?

December 5, 2013 at 3:38 am 13330
Danilo Danilo

I publish my code, because a lot of ways, but my custom field is saved blank:
[php]
function render_posttitle( $form_id, $post_id, $form_settings ) {
$value = get_post_meta( $post_id, ‘post_title’, true );
update_post_meta( $post_id, ‘meta_posttitle’, $value );
?>
<div class="wpuf-fields">
<input type="hidden" name="my_posttitle" value="<?php echo esc_attr( $value ); ?>">
</div>
<?php
}
add_action( ‘hook_render_posttitle’, ‘render_posttitle’, 10, 3 );

/* Write meta key */
function get_posttitle( $post_id ) {
if ( isset( $_POST[‘my_posttitle’] ) ) {
update_post_meta( $post_id, ‘meta_posttitle’, $_POST[‘my_posttitle’] );
}
}

add_action( ‘wpuf_add_post_after_insert’, ‘get_posttitle’ );
add_action( ‘wpuf_edit_post_after_update’, ‘get_posttitle’ );
[/php]

The code save always a blank key.

December 5, 2013 at 3:49 am 13331
Danilo Danilo

Sorry, here is the code:
http://pastebin.com/Zm5wEdY5

Always a get a blank key.

December 5, 2013 at 9:22 am 13340
Danilo Danilo

Definitively it doesn’t work.
Sorry, I’m trying all possibile combinations, I get always a blank key.

December 5, 2013 at 11:55 am 13348
Tareq Hasan Tareq Hasan

The post title in WordPress is not a meta field. So you can’t get the post title via get_post_meta(), use get_post_field() for retrieving the post title.

December 6, 2013 at 9:29 am 13401
Danilo Danilo

Sorry, but I can’t make this code to work!
Why so difficult?

See latest version of my code here:
http://pastebin.com/EktsAiQD

How is wrong?

Viewing 15 Posts - 1 through 15 (of 18 total)