ACF Field not saving data - wordpress

After changing the field value to a new one, it does not save data. I tried update value via function update_field(), but not actions
$post_id = 197;
$value = update_field( 'my_field', 'my_value', $post_id );

You should use field key instead of field name,
try this code.
$post_id = 197;
//Repace field_56e683ab6265f with your field key
update_field( 'field_56e683ab6265f', 'my_value', $post_id );
Or You can also do with this
update_post_meta( $post_id , 'my_field', 'my_value' );
//Repace field_56e683ab6265f with your field key
update_post_meta( $post_id , '_my_field', 'field_56e683ab6265f' );

Related

Take multiple ACF user fields and combine them into one

I have some ACF user fields on posts where users can be selected, I would like to take those fields and combine them into another field. So basically the users in field 1 and field 2 show up in field 3. Here is what I have tried so far. The return format for the fields is user array.
add_action( 'acf/save_post',
'my_acf_save_post', 10 );
function my_acf_save_post( $post_id ) {
// Get value of field 1
$value1 = get_field( 'contractor',
$post_id );
// Get value of field 2
$value2 = get_field( 'architect',
$post_id );
update_field( 'project_user_select',
$value1 . $value2, $post_id );
}
I was able to get this to work using array_merge.
function my_acf_save_post_31( $post_id ) {
// Get new value of field 1
$value1 = get_field( project_manager', $post_id, false );
// Get new value of field 2
$value2 = get_field( 'architect', $post_id, false );
// Get new value of field 3
$value3 = get_field( 'contractor', $post_id, false );
$merge = array_merge($value1,$value2,$value3);
update_field( 'project_user_select', $merge, $post_id );// update
'other_field' with 'my_field' value
}
add_action( 'acf/save_post', 'my_acf_save_post_31', 10 );
If there is a possibility of one of the values being empty I would write it something like this:
// Get new value of field 1 or set value to null
if(!empty(get_field( 'project_manager' )) {
$value1 = get_field( project_manager', $post_id, false );
} else {
$value1 = '';
}
What this does is check if the field has entries, if it does it will assign the value to the variable $value. If the field does not have a value it will just declare the variable so when you try to add each of the $values to $merge it will still work because the variable has been declared.

show wp_postmeta in woocommerce order dashboard

i try to show a specific meta_value based on post_id and meta_key in woocommerce order dashboard:
First i create a new column:
function mb_set_order_note_column( $columns ) {
$columns['parcel_delivery'] = __('DHL','TEXTDOMAIN');
return $columns;
}
add_filter( 'manage_shop_order_posts_columns', 'mb_set_order_note_column', 99 );
After i have tried to get the specific value from wp_postmeta database
function mb_show_order_note_columns( $column_name, $post_id ) {
switch ( $column_name ) {
case 'parcel_delivery':
$order = new WC_Order( $post_id );
$delivery = get_post_meta( $post_id, '_parcel_delivery_opted_in' );
print $delivery ;
break;
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'mb_show_order_note_columns', 10, 2 );
this shows only the customer notes from wp_post database. how to change to get specific value...
Thx
I see your custom meta key is _parcel_delivery_opted_in. So to get this value from wp_postmeta with order id and meta key,
$delivery = get_post_meta( $post_id, '_parcel_delivery_opted_in', true ); // like _billing_email meta
3rd parameter true will return single value, if you omit third parameter an array will be returned.

Wordpress update button works but not publish button

I asked the programmer to have the title of every post automatically be the date selected and the place selected (to allow users to save some time). When I click 'publish' the title of the post is the date twice (ie; 03-26-15 03-26-15). Then the 'publish' button turns into an 'update' button. And then when I click 'update' the title will be correct (ie; 03-26-15 London). I'm trying to figure out how to get it this way after clicking 'publish' the first time. The programmer disappeared and I can't figure it out. Any help would be great.
function post_updated( $post_id ) {
global $post_type;
if ($post_type == 'place') {
if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
$postTitle.=get_the_date( 'm/d/Y', $post_id );
$postTitle.=" ".get_the_title(get_post_meta($post_id, 'place_locale', true) ) ;
$post['ID'] = $post_id;
$post['post_title'] = $postTitle;
remove_action( 'save_post', 'post_updated' );
wp_update_post($post);
add_action( 'save_post', 'post_updated' );
}
}
}
add_action( 'save_post', 'post_updated' );
The .= operator appends, therefor your titles are being duplicated.
Simply change it to = and you should be fine.
Are you sure this is the only function? It seems to only have "update post" code.
Post creation is handled with wp_insert_post something like:
$my_post = array(
'post_title' => $title,
'post_content' => "",
'post_status' => "pending",
'post_author' => wp_get_current_user()->ID
);
$post_id = wp_insert_post( $my_post );
Old question, but this may help who got into this question:
There are some problems with the code:
global $post_type is not defined.
As mentioned by #dojs, you should use = operator instead of .= to define the variable first.
get_the_title() function only accepts post_id or post object as the param.
Since you're dealing with a custom post type of place, it's better to use a post type specific hook instead of the general and crowded save_post hook.
After these modifications, the final code could be something like this:
function tst_post_updated( $post_id )
{
if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
$post_title = get_the_date( 'm/d/Y', $post_id );
$post_title .= " " . get_post_meta($post_id, 'place_locale', true);
$post_title = sanitize_title($post_title);
$post['ID'] = $post_id;
$post['post_title'] = $post_title;
remove_action( 'save_post_place', 'tst_post_updated' );
wp_update_post($post);
add_action( 'save_post_place', 'tst_post_updated' );
}
}
add_action( 'save_post_place', 'tst_post_updated' );

how update post meta when post type post edit and save

hi all here is my function
function save_urun_meta_price( $post_id ) {
$slug = 'urun';
if ( $slug != $_POST['post_type'] ) {
return;
}
// - Update the post's metadata.
if ( isset( $_REQUEST['urun_indirimli_fiyat'] ) ) {
$product_price = get_post_meta( $post_id, 'urun_fiyat', true );
$product_discount = of_get_option('urun_discount');
$yuzde = ($product_discount / 100)*$product_price;
$discounted = $product_price-$yuzde;
update_post_meta( $post_id, 'urun_indirimli_fiyat', $discounted );
}
}
add_action( 'save_post', 'save_urun_meta_price' );
add_action( 'edit_post', 'save_urun_meta_price' );
When user write some price into the urun_fiyat meta field i want to calculate this price with % discount field from the options framework panel.
Than i want to put new price another meta field urun_indirimli_fiyat..
What is wrong with my function ?
Thanks.
Try using the below code. I think the problem was with the product price variable. You were trying to get the value from post meta ( which does not exist )
Getting the value from $_POST variable will do the trick I guess.
function save_urun_meta_price( $post_id ) {
$slug = 'urun';
if ( $slug != $_POST['post_type'] ) {
return;
}
// - Update the post's metadata.
if ( isset( $_REQUEST['urun_indirimli_fiyat'] ) ) {
$product_price = $_POST['urun_fiyat'];
$product_discount = of_get_option('urun_discount');
$yuzde = ($product_discount / 100)*$product_price;
$discounted = $product_price-$yuzde;
update_post_meta( $post_id, 'urun_indirimli_fiyat', $discounted );
}
}
add_action( 'save_post', 'save_urun_meta_price' );
add_action( 'edit_post', 'save_urun_meta_price' );

Wordpress - Check if post exists,if yes update fields

I use a frontend form to post, and what i want is to check if a post exists by title.If yes and metafield value is bigger than the old one,just replace the value.
Anyone that has implemented something like that in the past?
<?php
session_start();
$user_email = $_SESSION['user_email'];
$user_name = $_SESSION['user_name'];
$user_img_url = 'https://graph.facebook.com/'.$user_name.'/picture?width=200&height=200';
global $wpdb;
global $post;
$title = $user_name; // get the inputted title
$content = $_POST['content']; // get the inputted content
$categorie = $_POST['cat']; // get the category selected by user
$zombies = $_POST['zombies'];
$kliks = $_POST['klik'];
$timess = $_POST['times'];
$name = $_POST['namn'];
if( 'POST' == $_SERVER['REQUEST_METHOD'] ) { // if form has been submitted
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 2,
'post_category' => array(2),
);
$my_post = wp_insert_post($my_post);
add_post_meta($my_post, 'Zombies', $zombies);
add_post_meta($my_post, 'klik', $kliks);
add_post_meta($my_post, 'times', $timess);
add_post_meta($my_post, 'namn', $name);
add_post_meta($my_post, 'profile_photo', $user_img_url);
wp_redirect( home_url() );
# if $verifica is not empty, then we don't insert the post and we display a message
}
?>
Your question is not quite clear to me but if you want to query a post by it's title then you can use get_page_by_title() function as given below
$post = get_page_by_title( $_POST['post_title'], OBJECT, 'post' );
To get a custom meta field you can use get_post_meta() function as given below
$meta_value = get_post_meta($post->ID, 'field_name', true);
Then compare and update the meta value you can use, for example,
if( $_POST['custom_meta_field'] > $meta_value )
{
// Update the meta value
update_post_meta( $post->id, 'field_name', $meta_value );
}
The update_post_meta() function is being used to update the custom meta field.
Update: (Based on comment)
You can use following to get the post that is available by Facebook ID
$post = get_page_by_title( $_POST['facebook_id'], OBJECT, 'post' );
Also if the meta field is time string (12:10 am) then you have to convert it to timestamp/numeric value before you compare it, like,
$meta_value = strtotime(get_post_meta($post->ID, 'field_name', true));
So, it'll become something like 1363493400 and you can compare like
if( $_POST['custom_meta_field'] > $meta_value ){ ... }
In this case your custom_meta_field should be also a timestamp/numeric value or you have to convert it using strtotime() function just like $meta_value has been converted.

Resources