I have a problem with the acf_form for the custom titles, ie they do not work.
It works perfectly when it is with the wordpress control panel (wp-admin) but not on custom pages
My code is:
functions.php
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$posttypes = array( 'post' );
$currentposttype = get_post_type();
if ( in_array( $currentposttype, $posttypes ) ) { //only run if is certain post-type
if( $currentposttype == 'post') {
$marca = get_field('brand');
$marcaname = $marca->name;
$modelo = get_field('model');
$modeloname = $modelo->name;
$new_title = $marcaname . ' ' . $modeloname . ' ' . get_field('year');
$my_post['post_title'] = $new_title;
}
//Unhook function to prevent infitnite looping
remove_action('acf/save_post', 'my_post_title_updater', 20);
// Update the post into the database
wp_update_post( $my_post );
//Rehook function to prevent infitnite looping
add_filter('acf/save_post', 'my_post_title_updater', 20);
}
}
add_action('acf/save_post', 'my_post_title_updater', 20);
page.php
<?php
acf_form_head();
get_header();
?>
<form id="post" class="acf-form" action="" method="post">
<div class="item">
<h2>Type of Vehicle</h2>
<div class="groups-three">
<?php
acf_form(
array(
'field_groups' => array('group_58a4ca2b7f3bf'),
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'post',
'post_status' => 'publish'
),
'form' => false
));
?>
</div>
</div>
<div class="item">
<h2>Basic data</h2>
<div class="groups-two">
<?php
acf_form(
array(
'field_groups' => array('group_589884748e2bb'),
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'post',
'post_status' => 'publish'
),
'form' => false
));
?>
</div>
<?php
acf_form(
array(
'field_groups' => array('group_589a98b6015e5'),
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'post',
'post_status' => 'publish'
),
'form' => false
));
?>
</div>
<div class="item price">
<h2>Price US$</h2>
<?php
acf_form(
array(
'field_groups' => array('group_589a25ddec8fc'),
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'post',
'post_status' => 'publish'
),
'form' => false
));
?>
</div>
<div class="item">
<h2>Location</h2>
<div class="groups-three">
<?php
acf_form(
array(
'field_groups' => array('group_589a3018925f1'),
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'post',
'post_status' => 'publish'
),
'form' => false
));
?>
</div>
</div>
<div class="item gallery">
<h2>Gallery</h2>
<?php
acf_form(
array(
'field_groups' => array('group_589a25a57c10c'),
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'post',
'post_status' => 'publish'
),
'form' => false
));
?>
</div>
<div class="item">
<h2>Contact information</h2>
<div class="groups-two">
<?php
acf_form(
array(
'field_groups' => array('group_589a2bba2de29'),
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'post',
'post_status' => 'publish'
),
'form' => false
));
?>
</div>
</div>
<div class="acf-form-submit">
<input type="submit" class="acf-button button button-primary button-large" value="Submit">
<span class="acf-spinner"></span>
</div>
</form>
<?php get_footer(); ?>
Have you tried specifying the post ID when getting the fields you want to use for your title?
Instead of
$marca = get_field('brand');
Try
$marca = get_field('brand', $post_id);
I don’t see why it’s not working, other than possibly things getting confused because of the multiple forms.
Have you tried this with all of the field groups included in a single acf_form()?
I’ve tried changing the code as you suggest. But it has not worked that way. Excuse my English, I speak Spanish.
at the beginning of your function
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$posttypes = array( 'post' );
$currentposttype = get_post_type($post_id); // add $post_id here
I put the correction you indicated, but it does not work with the acf_form
The topic ‘Custom titles not working acf_form false’ is closed to new replies.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.