Hi
I am creating a front end submission form which is working great so far. What I’d love is to create the actual post title (and URL) on the fly using some of the data from the custom fields.
For example; it would use the post’s ID and then if a user chose certain options from a couple of dropdown’s then the title may become this:
Note: I am using ACF Pro (5)
http://www.domain.com/listings/post-id-number/france/barcelona/
or perhaps
http://www.domain.com/lsitings/post-id-number-france-barcelona/
here is my code:
<?php
acf_form(array(
'post_id' => 'new_post', //create a new post (listing)
'new_post' => array(
'post_type' => 'listing',
'post_status' => 'publish',
'post_title' => $_POST['acf']['field_56c729b70f047'],
),
'post_title' => false,
'post_content' => false,
'field_groups' => array(69),
'submit_value' => __('Submit Listing', 'hciw'),
'updated_message' => __('Updated Listing', 'hciw'),
'return' => home_url() . '/my-account/?listingsubmission=success', //redirect users after post submission
));
?>
Any help would be most welcome.
EDIT: I added 'post_title' => $_POST['acf']['field_56c729b70f047'],
but still get a blank title stored.
Hi @huwrowlands
I’m afraid this structure needs you to edit your permalink settings on “Settings > Permalinks”:
http://www.domain.com/listings/post-id-number/france/barcelona/.
For the second link structure, i believe you can use acf/save_post hook to do it.
You can use wp_update_post() function based on the custom field. Be caution with the infinite loop, though. This page should give you more idea about it: https://codex.wordpress.org/Function_Reference/wp_update_post#Caution_-_Infinite_loop.
I hope this helps.
I’v tried adding this to the head of my template file (and also in functions.php) but still no title is created?
function my_pre_save_post( $post_id ) {
/*var_dump( $post_id ); // OUTPUT: NULL
var_dump( $_POST['post_id'] ); // OUTPUT: string(3) "new"
die();*/
if ( $post_id != 'new_post' ) {
return $post_id;
}
$post = array(
'post_title' => 'A custom title',
);
$post_id = wp_insert_post( $post );
return $post_id;
}
add_filter( 'acf/pre_save_post', 'my_pre_save_post', 10, 1 );
Any other ideas?
Thanks for the help.
The topic ‘Custom Post Title with Front End Form ?’ 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.