Tagged: 

Viewing 8 reply threads
  • Author
    Posts
    • #1685
      kattagami
      Member

      Hi,

      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?

    • #1687
      Steve
      Keymaster

      @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_data function. 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_array array 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.

    • #1688
      kattagami
      Member

      Thank 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 😉

    • #1689
      Steve
      Keymaster

      @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.

    • #1690
      kattagami
      Member

      With 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 🙂

    • #1719
      kattagami
      Member

      Hello Steve,

      You said :

      “Open class-piklist-cpt.php and find the wp_insert_post_data function. 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.

    • #1720
      Steve
      Keymaster

      @kattagami– This fix is just in another branch in our repo. It will be in the next major version of Piklist.

    • #5056
      tatamata
      Member

      Hello @sbruner and @kattagami,

      Is there a way to get the value of the field, in frontend form, before hitting submit?

    • #5063
      kattagami
      Member

      Hello @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/

Viewing 8 reply threads
  • The topic ‘How to get metabox data from the form before to save a new post?’ is closed to new replies.