Tagged: fronted forms
- This topic has 8 replies, 3 voices, and was last updated 6 years, 2 months ago by
kattagami.
-
AuthorPosts
-
-
May 1, 2014 at 9:58 am #1685
kattagamiMemberHi,
I’ve got a custom post type “organizer” and I’m trying to find a way to set the title of each post based on two fields data value group in a metabox. (I hid the title field)
See this screenshot: http://i.imgur.com/mqzRpDM.png
I use Piklist (I’ve created the CPT in a piklist plugin) and tried this code on the hook “publish_{post-type-slug}”:
function set_cpt_organizer_title( $post_id ) { // If this is a revision, get real post ID if ( $parent_id = wp_is_post_revision( $post_id ) ) $post_id = $parent_id; $name_group = get_post_meta( $post_id, 'name_group', true); if ( !empty( $name_group ) ) { $full_name = $name_group['firstname'][0] .' '. $name_group['lastname'][0]; else { $full_name = 'You forgot the name'; } $my_post = array( 'ID' => $post_id, 'post_title' => $full_name, 'post_status' => 'publish', 'post_type' => 'organizer' ); // unhook this function so it doesn't loop infinitely remove_action( 'publish_organizer', 'set_cpt_organizer_title' ); // update the post, which calls save_post again wp_update_post( $my_post ); // re-hook this function add_action( 'publish_organizer', 'set_cpt_organizer_title' ); //} } add_action( 'publish_organizer', 'set_cpt_organizer_title' );As you can see, I use “get_post_meta()” but the first time I add a new post, piklist data field are not yet in the database so title is set to “You forgot the name”.
My question: How to get, from the piklist form, the data fields “firstname” and “Lastname” before they are saved in the database?
-
May 1, 2014 at 1:37 pm #1687
SteveKeymaster@kattagami– Piklist actually has a filter to help you do this:
piklist_empty_post_title. I just tested it and there is a small bug which I fixed, and will be in the next version of Piklist. Try this:1) Open class-piklist-cpt.php and find the
wp_insert_post_datafunction. Change the IF statement to this:
if (($data['post_status'] != 'auto-draft') && (($data['post_title'] == 'Auto Draft') || empty($data['post_title']))). That fixes the bug.2) Here’s your function:
function set_cpt_organizer_title($data, $post_array) { if ($post_array['post_type'] == 'organizer') { if((isset($post_array['_post_meta']['firstname'][0])) || isset($post_array['_post_meta']['lastname'][0])) { $full_name = $post_array['_post_meta']['firstname'][0] . ' ' . $post_array['_post_meta']['lastname'][0]; return $full_name; } else { return $post_array['post_title']; } } else { return $post_array['post_title']; } } add_filter('piklist_empty_post_title', 'set_cpt_organizer_title', 10, 2);3) The WordPress
$post_arrayarray returns all the data from your post including meta, so you don’t need to use get_post_meta.Let me know if this works. If there are no issues we’ll document it and write a tutorial.
-
May 1, 2014 at 6:27 pm #1688
kattagamiMemberThank you Steve for your help.
Your code works with a few change. Because my two fields are embed in a “name_group” group field, I replaced
$post_array['_post_meta']['firstname'][0]
by
$post_array['_post_meta']['name_group']['firstname'][0]But now I can’t update the title: if I edit and change my fields ‘first and lastname’ the title doesn’t change.
I think I can resolve this with ‘wp_update_post()’. I’m going to investigate this idea next sunday, I will be to the Zurich Wordcamp on friday and saturday 😉 -
May 1, 2014 at 8:48 pm #1689
SteveKeymaster@kattagami– You’re really close! Take a look at the piklist filter you updated,
wp_insert_post_data. The conditional we have in there specifically says not to only update the title if empty($data[‘post_title’]). Copy this function, rename it, and place it in your plugin/theme. Then change that conditional. It will work. -
May 5, 2014 at 10:33 am #1690
kattagamiMemberWith my first function and your solution, I can now add and update my title’s custom Post the way I described in my first message.
Thanks 🙂
-
May 14, 2014 at 6:49 am #1719
kattagamiMemberHello Steve,
You said :
“Open
class-piklist-cpt.phpand find thewp_insert_post_datafunction. Change the IF statement to this:
if (($data['post_status'] != 'auto-draft') && (($data['post_title'] == 'Auto Draft') || empty($data['post_title'])))That fixes the bug.”Do you plan to change the piklist core in a next update with this code ?
I don’t see the change in your last 0.9.3.3 version. -
May 14, 2014 at 10:58 am #1720
SteveKeymaster@kattagami– This fix is just in another branch in our repo. It will be in the next major version of Piklist.
-
November 22, 2015 at 12:34 pm #5056
tatamataMemberHello @sbruner and @kattagami,
Is there a way to get the value of the field, in frontend form, before hitting submit?
-
November 23, 2015 at 8:08 am #5063
kattagamiMemberHello @tatamata,
Yes, there is a way to get the value of the field, in frontend form, before hitting submit
Check this thread:https://piklist.com/support/topic/build-a-contact-form-with-piklist-0-9-9-6/
-
-
AuthorPosts
- The topic ‘How to get metabox data from the form before to save a new post?’ is closed to new replies.