Support

Account

Home Forums Feature Requests Set 'Post Title' from Field Group (Toggle)

Solved

Set 'Post Title' from Field Group (Toggle)

  • Hi,

    It would be useful to be able to set the post title from the field group editor, without getting your hands on any code. (With the intention of being able to hide the default title field.)

    Something along the lines of a check box in a field, that will dually set that field as the title.

    Thanks,
    Andy.

  • Hi @andycheeseman

    Thanks for the request but this seems like a double up of functionality. The post edit screen already contains a field for editing the title.

    Please remember this plugin is for custom fields, not for WP core.

    Perhaps there is some other reason for this request?

    Thanks
    E

  • Hi,

    True, but I can think of a few situations where it may be beneficial to have some control of the title from within ACF.

    Control – To be able to append / prepend details to the title. Or to validate data (such as when you only want a drop down date entered – separate to the core date).

    Styling – To be able to control the look of title in the backend with similar ease as ACF. (Such as adding a ‘title’ to the title field.)

    Position – From the end users POV, it may make more sense to have the title field positioned between other fields.

    After sleeping on it, it may be useful to have the title generated from a combination of fields. So a ‘testimonial’ custom field group may save the title from a combination of fields. From a name field, followed by a separator, then the first 5 words from a testimonial field. (Less for use in the front end, more for ease of management in the backend).

    What do you think?

    Fantastic plugin, beautiful site. Keep up the great work!

    Thanks,
    Andy.

  • @andycheeseman: i’m using this method:

    
    function get_acf_title( $post_id ){
        $title = get_field( 'title', $post_id );
    
        if( empty( $title ) ){
          $title = get_the_title( $post_id );
        }
    
        if( $title == '-' ){
          $title = '';
        }
    
        return apply_filters( 'the_title', $title );
      }

    (Also contain the ability to leave the title completely blank)

  • Andy, I converted the method into a standalone function (it was used in a bigger class).

    You can use it by simply calling get_acf_title( get_the_ID() )

    On ACF admin you add a metabox with title field (which is set as a text field).

    Here is a quick view of the admin part:
    http://img.iamntz.com/jing/2014-01-30-15@15h24_4.png

  • @Ionut Staicu – Does the code you provided just go in the functions.php?

  • I’m looking to replace the actual WP ‘title’ of the post.

    So that it would reflect here…

  • @Andy: yes, that would go into functions.php. I’m not sure if it’s a good idea to remove title completely, at least not with my method.

    You could add a save hook that updates the title based on the field, but i think it would be a bit of an overkill.

  • Hi,

    This appears to do the job. Expanding on the theme of testimonials, it will update the actual WP post title based on two ACF fields, ‘name’ and ‘testimonial’. It will set the title as follows…

    NAME – “First 20 characters…”

    function my_post_title_updater( $post_id ) {
    
    	if ( get_post_type() == 'testimonial' ) {
    
    		$my_post = array();
    		$my_post['post_title'] = get_field( 'name', $post_id ) . ' - "' . substr(get_field( 'testimonial', $post_id ), 0, 30) . '..."';
    		
    		// Update the post into the database
    		wp_update_post( $my_post );
    
    	}
    
    }
     
    // run after ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_post_title_updater', 20);

    You could sharpen up the shortening of the testimonial, but everything I need to solve the challenge I had, seems to be achievable with this code.

    For prosperity, I’m using the ‘Custom Post Type UI‘ plugin to register the custom post types.

    Thanks for everyones help and input.

  • I was a little too early with the champagne…

    My function only appears to update existing posts correctly, not new posts. If I remove the function, create a new post, add the function in again and re-save, it works. Just not ‘off the bat’.

    Does anyone know which direction I should be looking in to resolve this?

    Thanks,
    Andy.

  • Hi @andycheeseman

    It is most likely the line: if ( get_post_type() == 'testimonial' ) { that is failing when you first save the post.

    Perhaps changing it to: if ( get_post_type($post_id) == 'testimonial' ) { will fix the issue.

    Thanks
    E

  • Hi Elliot,

    I’ve updated accordingly, but the problem remains. I’ve made a video of how it behaves and copied the code I’m using below. It all seems pretty kosher. Could it be something to do with the post_id not being ready for new posts?

    http://www.youtube.com/watch?v=tzkWP_SBRjU

    function my_post_title_updater( $post_id ) {
    
    	if ( get_post_type( $post_id ) == 'testimonial' ) {
    
    		$my_post = array();
    		$my_post['post_title'] = get_field( 'name', $post_id ) . ' - "' . substr(get_field( 'testimonial', $post_id ), 0, 30) . '..."';
    		
    		wp_update_post( $my_post );
    
    	}
    
    	if ( get_post_type( $post_id ) == 'equipment' ) {
    
    		$my_post = array();
    		$my_post['post_title'] = get_field( 'name', $post_id );
    		
    		wp_update_post( $my_post );
    
    	}
    
    }
     
    // run after ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_post_title_updater', 20);

    Thanks,
    Andy.

  • I wasn’t setting the ID into the my_post array.

    I’ve added that in and it works now for both new and updated posts. Weird how it updated for existing items but now new items.

    	if ( get_post_type( $post_id ) == 'equipment' ) {
    
    		$my_post = array();
    		$my_post['ID'] = $post_id;
    		$my_post['post_title'] = get_field( 'name', $post_id );
    		
    		wp_update_post( $my_post );
    
    	}
  • I’ve been playing around with this code a bit.
    But still experience 1 small issue with revisions.

    When posting a new page you always get 2 versions of the page.
    Version 1 sets all the acf data, and then version 2 changes the title.
    Would there be any way to hide the second version, or combine the 2?

    For prosperity, here’s the code I’ve been using in functions.php:

    /*  ACF Title Updater
    /* ------------------------------------ */
    // add_action( 'init', 'calmah_remove_post_title', 10 );
    function calmah_remove_post_title() {
        remove_post_type_support( 'post', 'title' );
    }
    
    add_action('acf/save_post', 'calmah_title_updater', 20);
    function calmah_title_updater($post_id) {
        $title = '';
        
        switch (get_post_type($post_id)) {
            case 'news':
                $title = get_field('nl_title', $post_id); break;
            case 'events':
                $title = get_field('date_start', $post_id) . ': ' . get_field('nl_title', $post_id); break;
            case 'album':
                $title = get_field('nl_title', $post_id) . '(' . get_field('location', $post_id) . ')'; break;
            case 'members':
                $title = get_field('first_name', $post_id) . ' ' . get_field('family_name', $post_id); break;
        }
        
        if (!empty($title)) {
            $slug = sanitize_title($title);
        	$my_post = array(
    			'ID' => $post_id,
    			'post_title' => $title,
    			'post_name' => $slug
    		);
    		wp_update_post($my_post);
        }
    }
  • Hi all,

    This has been really useful for me. I’ve been trying to find a way to use an ACF field as a CPT title and this works nicely for me

    `* add custom slug and title */

    function my_post_title_updater( $post_id ) {

    if ( get_post_type( $post_id ) == ‘venue’ ) {

    $my_post = array();
    $my_post[‘ID’] = $post_id;
    $my_post[‘post_title’] = get_field( ‘venue_name’, $post_id );

    wp_update_post( $my_post );

    }

    }

    // run after ACF saves the $_POST[‘fields’] data
    add_action(‘acf/save_post’, ‘my_post_title_updater’, 20);

    A couple of quick questions …

    How can I concatenate two variables so that I can use ‘venue_name’ and ‘venue_city’?

    Also, how can I expand this to update the slug to use the same title ‘venue_name’ and ‘venue_city’? I currently have http://localhost:8888/gigs/venue/auto-draft/ as the slug.

    Thanks all.

    Cheers

    Phil

  • Did you get an answer to this question? i also want to accomplish this.

    Title = {field1} {field2} {field3} {field4}… i added your code below. i hope it is as easy as this..

    Any ideas??

    * add custom slug and title */

    function my_post_title_updater( $post_id ) {

    if ( get_post_type( $post_id ) == ‘venue’ ) {

    $my_post = array();
    $my_post[‘ID’] = $post_id;
    $my_post[‘post_title’] = get_field( ‘venue_name’, ‘field2’, ‘field3’, $post_id );

    wp_update_post( $my_post );

    }

    }

    // run after ACF saves the $_POST[‘fields’] data
    add_action(‘acf/save_post’, ‘my_post_title_updater’, 20);

  • Hi Scot,

    Not yet. TBH I haven’t spent much time on this since as another project has to be finished first. I’ll be picking it up again in a couple of weeks. Will let you know how I go.

  • Hi guys, firstly thank you for providing your solution to this – I’m trying to do the same thing but I need to use an ACF field’s related taxonomy in the Post Title…

    When I implement your code @andycheeseman I can pull in the ID of the field I want, but not the Object. I have tried setting the Return Value in the field’s settings to Term Object rather than Term ID but I still get the ID.

    For reference this is the code I am using:

    function my_post_title_updater( $post_id ) {
    
    	if ( get_post_type() == 'match' ) {
    
    		$my_post = array();
    		$my_post['ID'] = $post_id;
    		$my_post['post_title'] = get_field( 'home_team', $post_id );
    		
    		// Update the post into the database
    		wp_update_post( $my_post );
    
    	}
    
    }
    
    // run after ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_post_title_updater', 20);

    `

    Any help on how to get the taxonomy values from an ACF field would be much appreciated!!

    Thanks 🙂

Viewing 20 posts - 1 through 20 (of 20 total)

The topic ‘Set 'Post Title' from Field Group (Toggle)’ is closed to new replies.