@mkeys Thank you! This working fine on Advanced Custom Fields PRO v. 5.5.11
Please add this feature! I also really need it!
Thank you, my problem is solved!
I understand the use of “true” and “false.” But how to use a dynamic value? For example, I have more than ten types of custom posts, and I just want to enter the name of the custom post type in the shortcode. For example, [acfNew acfposttype=”books”] or [acfNew acfposttype=”food”] or [acfNew acfposttype=”abc123″].
I want to display a ACF frontend form using the shortcode like this:
[acf-new acfpostidid=”new_post” acfposttitle=”true” acfpostcontent=”true” acfposttype=”post” acfpoststatus=”publish” acfpostreturn=”%post_url%” acfpostsubmit=”Public”]
How do I modify the code to do this?
Author plugin answered in this topic.
This code works.
function acf_set_featured_image( $value, $post_id, $field ){
$id = $value;
if( ! is_numeric( $id ) ){
$data = json_decode( stripcslashes($id), true );
$id = $data['cropped_image'];
}
update_post_meta( $post_id, '_thumbnail_id', $id );
return $value;
}
// acf/update_value/name={$field_name} - filter for a specific field based on it's name
add_filter( 'acf/update_value/name=fieldname', 'acf_set_featured_image', 10, 3 );
I checked it out. For example.
If the image is not cropped:
string(3) "929"
If the image has been cropped:
int(991)
I apologize for clarification. But is there a way to use this code only to certain types of posts, in particular, only the pages?
Sumptuously! I am very grateful to you!
James, I thank you for your help. It was a long topic, but I hope it will be useful to the user in the creation of ACF front-end forms.
Thank you, it works. But number fields is not very convenient. There are ACF addon: Date and Time Picker Field. It displays the date and time field. It may be possible to modify this part of the code for it:
// get new value
$theDate = get_field('post-date', $post_id, false);
$theDate = $theDate . " " . get_field('dummy_hour', $post_id, false);
$theDate = $theDate . ":" . get_field('dummy_minute', $post_id, false);
if($theDate){
$theDate = date_create_from_format('Ymd H:i', $theDate);
$theDate = date_format($theDate, 'Y-m-d H:i:s');
$my_post = array(
'ID' => $post_id,
'post_date' => $theDate,
);
With this plugin, you can use only one field. The plugin can display the date and time in any format:
Date and time field appears good.
You can modify the code for one field? And say what format the date and time settings to use this code?
Thank you!
It only works with the following modification:
<?php
$galleyPost = get_field('post_gallery_field_name', false, false);
$image_ids = get_field('photo', $galleyPost, false);
$shortcode = '[gallery ids="' . implode(',', $image_ids) . '"]';
echo do_shortcode( $shortcode );
?>
Strangely enough, but the code does not work either. After the publication sets the current date and time. I use:
function my_acf_save_post( $post_id ) {
// get new value
$theDate = get_field('post-date', $post_id, false);
$theDate = $theDate . "|" . get_field('dummy_hour', $post_id, false);
$theDate = $theDate . ":" . get_field('dummy_minute', $post_id, false);
if($theDate){
$theDate = date_create_from_format('Ymd|H:i', $theDate);}
$my_post = array(
'ID' => $post_id,
'post_date' => $theDate,
);
// do something
// unhook this function so it doesn't loop infinitely
remove_action('acf/save_post', 'my_acf_save_post');
// update the post, which calls save_post again
wp_update_post( $my_post );
// re-hook this function
add_action('acf/save_post', 'my_acf_save_post');
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
At the same time, this code works: the date varies.
function my_acf_save_post( $post_id ) {
if(!is_admin()){
// get new value
$theDate = get_field('post-date', $post_id, false);
if($theDate){
$theDate = date_create_from_format('Ymd', $theDate);
$theDate = date_format($theDate, 'Y-m-d H:i:s');
$my_post = array(
'ID' => $post_id,
'post_date' => $theDate,
);
// do something
// unhook this function so it doesn't loop infinitely
remove_action('acf/save_post', 'my_acf_save_post');
// update the post, which calls save_post again
wp_update_post( $my_post );
// re-hook this function
add_action('acf/save_post', 'my_acf_save_post');
}
}
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
Thank you, I successfully hid the Date field.
On the second issue to no avail. I changed:
'post_date' => $theDate,
On this:
'post_date_gmt' => $theDate,
But in this case, the code is not work. What can be wrong?
1) Can I hide the date field in the form of backend? But keep in frontend form.
2) When editing the date fields does not match the time zone.
It works, thank you very much! But there are two notes, could you help?
1) If you edit the date field via the backend ACF form, post is not updated. An error 504 Gateway Time-out. At the same time, if you create or update post the frontend ACF form shape works well.
2) All times are not the same as the one used on the site.
It works!
Yes, but ‘post_content’ is not a field ACF. This is a required field post.
Semicolon is missing on the documentation page: http://www.advancedcustomfields.com/resources/acf-pre_save_post.
Yes, I changed the key date field.
Attach files:
The code is missing a semicolon.
// check if this is to be a new post
if( $post_id != 'new' ) {
return $post_id
}
It should be as follows:
// check if this is to be a new post
if( $post_id != 'new' ) {
return $post_id;
}
But I was not able to achieve success with your code. I use the Date Picker field with default settings. It is right?
Thanks for the help! I do not understand why your code calls a server 500 error.
Hmmm … frontend-form no longer appear on my site. I do not fully understand: this code is not used function acf_form(); ?
At the moment I’m using the following code:
acf_form(array(
'post_id' => 'new_post',
'post_title' => true,
'post_content' => true,
'new_post' => array(
'post_type' => 'post',
'post_status' => 'publish'
),
'return' => '%post_url%',
'submit_value' => 'Опубликовать'
));
Please give an example of code that runs fast selection of the date of publication.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.