Hi friends,
ACF v.5.6.10.
I have a form to create a custom post type called “animal”.
<?php acf_form_head(); ?>
<?php get_header();
acf_enqueue_uploader();
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php acf_form(array(
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'animal',
'post_status' => 'publish'
),
'submit_value' => 'Salvar'
)); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
In my functions.php i create the filter acf/pre_save_post to change the post_title to use a custo post field called Name.
add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
function my_pre_save_post( $post_id ) {
$newTitle = $_POST['acf']['field_5b32f9debf71e'];
// User is logged
//if ( !is_user_logged_in() ) {
// return;
//}
// check if this is to be a new post
if( $post_id != 'new_post' ) {
return $post_id;
}
// Verify if is the post_type 'animal'
if( get_post_type($post_id) == 'animal' ) {
// Create a new post
$post = array(
'post_type' => 'animal',
'post_status' => 'publish',
'post_title' => $newTitle
);
// insert the post // wp_update_post
$post_id = wp_insert_post($post);
return $post_id;
} // end-of-if ==> if( get_post_type($post_id) == 'animal' )
return $post_id;
}
So, when i verify the custom posts animal in admin area, the title is blank.
All examples i found use this code.
Thanks for the help.
Hi again friends,
I resolved this with the other hook:
add_action('acf/save_post', 'my_save_post', 20);
function my_save_post($post_id){
if( get_post_type($post_id) == 'animal' ) {
// Get the data from a field
$new_title = get_field('nome', $post_id);
// Set the post data
$new_post = array(
'ID' => $post_id,
'post_title' => $new_title,
);
// Remove the hook to avoid infinite loop. Please make sure that it has
// the same priority (20)
remove_action('acf/save_post', 'my_save_post', 20);
// Update the post
wp_update_post( $new_post );
// Add the hook back
add_action('acf/save_post', 'my_save_post', 20);
}
}
But the question is, why the code to insert post don´t work in acf/pre_save_post ???
Thank´s