Tagged: empty post title, two fields
- This topic has 10 replies, 3 voices, and was last updated 5 years, 5 months ago by
Steve.
-
AuthorPosts
-
-
August 22, 2016 at 5:19 am #7214
intrepidrealistParticipantHi!
I’ve been at this for a couple of hours. I found the code I needed to change empty title fields with Custom Post Types.
I can’t get the code to work.add_filter('piklist_empty_post_title', 'cazooz_new_title', 10, 2); function cazooz_new_title($data, $post_array) { // Grab the new title from the custom field. Replace my_custom_field with your own $new_title = $post_array['_post_meta']['child_first_name']; print_r($post_array); // output die(); // Only filter on your custom post type. Replace my_custom_cpt with your own. return $post_array['post_type'] == 'child' ? $new_title : $post_array['post_title']; }Registered CPT:
$args = array( 'description' => 'Child Post Type', 'show_ui' => true, 'query_var' => true, 'menu_position' => 4, 'exclude_from_search' => false, 'labels' => array( 'name'=> 'Children', 'singular_name' => 'Child', 'add_new' => 'Add New Child', 'add_new_item' => 'Add New Child', 'edit' => 'Edit Children', 'edit_item' => 'Edit Child', 'new-item' => 'New Child', 'view' => 'View Child', 'view_item' => 'View Child', 'search_items' => 'Search Children', 'not_found' => 'No Children Found', 'not_found_in_trash' => 'No Children Found in Trash', 'parent' => 'Parent Child' ), 'public' => true, 'publicly_queryable' => true, 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => true, 'has_archive' => true, 'taxonomies' => array('interest_tag', 'activity_type' ), 'supports' => array('custom-fields') ); register_post_type( 'child' , $args );Am I doing something wrong here?
Once I eventually get it to work, I’d like to fill the title from two fields: child_first_name and child_last_name. Is that possible?Thanks
-
August 22, 2016 at 4:36 pm #7218
jrcreativeMemberI just tested it and it works for me. You should be able to drop it into your functions.php you’re good to go as long as child_first_name’s value is a string.
And for getting both first and last name, you’ll want to do something like this:
$new_title = $post_array['_post_meta']['child_first_name'] . " " . $post_array['_post_meta']['child_last_name']; -
August 23, 2016 at 11:43 am #7232
SteveKeymaster@intrepidrealist– Did this work for you?
-
August 23, 2016 at 6:04 pm #7234
intrepidrealistParticipantHi @steve!
I haven’t had a chance to try it out. Will do so tonight or tomorrow!
Thanks for the follow up! -
August 24, 2016 at 6:20 pm #7237
intrepidrealistParticipantI have the code in my Functions Plugin. When I add a new child I get this in the browser window:
Array ( [post_author] => 1 [post_content] => [post_content_filtered] => [post_title] => Auto Draft [post_excerpt] => [post_status] => publish [post_type] => child [comment_status] => closed [ping_status] => closed [post_password] => [to_ping] => [pinged] => [post_parent] => 0 [menu_order] => 0 [guid] => http://www.cazooz.dev/?post_type=child&p=103 [import_id] => 0 [context] => [ID] => 103 [post_date] => 2016-08-24 22:17:01 [post_date_gmt] => [post_name] => [post_modified] => 2016-08-24 22:16:15 [post_modified_gmt] => 0000-00-00 00:00:00 [post_mime_type] => [comment_count] => 0 [ancestors] => Array ( ) [post_category] => Array ( ) [tags_input] => Array ( ) [_wpnonce] => fe14d72543 [_wp_http_referer] => /wp-admin/post-new.php?post_type=child [user_ID] => 1 [action] => editpost [originalaction] => editpost [original_post_status] => auto-draft [referredby] => http://www.cazooz.dev/wp-admin/ [_wp_original_http_referer] => http://www.cazooz.dev/wp-admin/ [auto_draft] => 1 [post_ID] => 103 [meta-box-order-nonce] => e26735a2ba [closedpostboxesnonce] => 2ba3925fcf [wp-preview] => [hidden_post_status] => draft [hidden_post_password] => [hidden_post_visibility] => public [visibility] => public [mm] => 08 [jj] => 24 [aa] => 2016 [hh] => 22 [mn] => 16 [ss] => 16 [hidden_mm] => 08 [cur_mm] => 08 [hidden_jj] => 24 [cur_jj] => 24 [hidden_aa] => 2016 [cur_aa] => 2016 [hidden_hh] => 22 [cur_hh] => 22 [hidden_mn] => 16 [cur_mn] => 16 [original_publish] => Publish [publish] => Publish [tax_input] => Array ( [interest_tag] => Array ( [0] => 0 [1] => 2 [2] => 4 ) [piklist_giggles_type] => Array ( [0] => 0 ) [activity_type] => Array ( [0] => 0 ) ) [newinterest_tag] => New Interest Tag Name [newinterest_tag_parent] => -1 [_ajax_nonce-add-interest_tag] => 6a3f8d51e8 [newpiklist_giggles_type] => New Giggles Type Name [newpiklist_giggles_type_parent] => -1 [_ajax_nonce-add-piklist_giggles_type] => c3eeffcffc [newactivity_type] => New Activity Type Name [newactivity_type_parent] => -1 [_ajax_nonce-add-activity_type] => 60dd7f32ca [metakeyselect] => #NONE# [metakeyinput] => [metavalue] => [_ajax_nonce-add-meta] => ff32035053 [_post_meta] => Array ( [child_first_name] => Kanye [child_last_name] => West [child_nick_name] => Kanny [gender] => male [birthday] => [weight] => [height] => [hair_color] => [eye_color] => [upload_basic] => Array ( [0] => ) [child_address] => [child_street_address_1] => [child_street_address_2] => [child_city] => santa_barbara [child_zipcode] => [school_name] => [school_grade] => [allergies] => [doctor_name] => [insurance_provider] => [policy_holder] => [emergency_name] => [emergency_phone] => ) [_taxonomy] => Array ( [child_interest] => ) [members_cp_meta] => 9993f2f987 [members_access_error] => [_] => Array ( [nonce] => 10d86e4a1f [fields] => 02bdcb4981be60b9034be83a69ea9113 ) [filter] => db )
I’m adding from the admin. It’s a local install, using DesktopServer.
-
August 24, 2016 at 6:38 pm #7238
jrcreativeMember@intrepidrealist this code in your functions.php should do the trick then.
add_filter('piklist_empty_post_title', 'cazooz_new_title', 10, 2); function cazooz_new_title($data, $post_array) { $new_title = $post_array['_post_meta']['child_first_name'] . " " . $post_array['_post_meta']['child_last_name']; return $post_array['post_type'] == 'child' ? $new_title : $post_array['post_title']; } -
August 24, 2016 at 6:40 pm #7239
intrepidrealistParticipantI will eventually have a front end form as well. Will this accommodate both?
-
August 24, 2016 at 6:42 pm #7240
intrepidrealistParticipantHa!!
I see where the problem was: I was printing the output.
Yay! Another Helen Keller “Water” moment! -
August 24, 2016 at 6:46 pm #7241
intrepidrealistParticipantThat worked!!!
-
August 24, 2016 at 6:52 pm #7242
jrcreativeMemberActually, it was the
die()statement that stopped the function from finishing. If you left theprint_rand removed thedie();it would have completed, but in a hook, it’s the only way to actually see the output, otherwise it wouldprint_rbehind the scenes and move on. You’d never see the result.Gotta love those learning moments!
-
August 29, 2016 at 8:36 pm #7274
SteveKeymaster@jrcreative– Thanks for the assist!
-
-
AuthorPosts
- The topic ‘Need to set empty title from two fields.’ is closed to new replies.