Hello,
Thanks for the feedback. The “Post Statuses” & “Post Types” fields are simply selector fields, they don’t automatically update the current Post.
Those fields are meant to developers which can easily embed the selector of their choice. That’s why you have multiples settings like: “Select multiples post types”, when it is not possible to actually have multiple post types for on single post.
I added the feature request to the Trello Board to let users automatically update the current post: https://trello.com/c/rOHK8pot/334-fields-post-types-statuses-setting-to-update-the-current-post
Have a nice day!
Regards.
Thanks for the fast reply! Now I see, so at a current state they are just a value loaders for developers.
>I added the feature request to the Trello Board to let users automatically update the current post
Would be great!
Hello,
Exactly! You can load/save the value to update the current post by yourself right now, using the following code:
/*
* Field: My Post Type - Load Value
*/
add_filter('acf/load_value/name=my_post_type', 'my_acf_post_type_load', 10, 3);
function my_acf_post_type_load($value, $post_id, $field){
// Bail early if not in a Post
if(!is_numeric($post_id))
return $value;
// Retrieve Current Post Type
$post_type = get_post_type($post_id);
// Force the Field value to load the Current Post Type
$value = $post_type;
// Return
return $value;
}
/*
* Field: My Post Type - Save Value
*/
add_filter('acf/update_value/name=my_post_type', 'my_acf_post_type_save', 10, 3);
function my_acf_post_type_save($value, $post_id, $field){
// Bail early if not in a Post
if(!is_numeric($post_id))
return $value;
// Update Current Post to the selected Post Type
wp_update_post(array(
'ID' => $post_id,
'post_type' => $value,
));
// Return null to not save the value as a meta
return null;
}
This is a simple example for the “Post Types” field selector.
Hope it helps!
Regards.
Thanks a lot for the code! For me it would take probably a few hours to write this.
And especially for reducing database bloating with useless values
// Return null to not save the value as a meta
In case someone with very low programming skills like me need it for the post status.
/*
* Field: My Post Status - Load Value
*/
add_filter('acf/load_value/name=post_status', 'acf_post_status_load', 10, 3);
function acf_post_status_load($value, $post_id, $field){
// Bail early if not in a Post
if(!is_numeric($post_id))
return $value;
// Retrieve Current Post Status
$post_status = get_post_status($post_id);
// Force the Field value to load the Current Post Status
$value = $post_status;
// Return
return $value;
}
/*
* Field: My Post Status - Save Value
*/
add_filter('acf/update_value/name=post_status', 'my_acf_post_status_save', 10, 3);
function my_acf_post_status_save($value, $post_id, $field){
// Bail early if not in a Post
if(!is_numeric($post_id))
return $value;
// Update Current Post to the selected Post Status
wp_update_post(array(
'ID' => $post_id,
'post_status' => $value,
));
// Return null to not save the value as a meta
return null;
}