Support

Account

Home Forums ACF PRO Delete WP Title and use ACF Title

Solved

Delete WP Title and use ACF Title

  • Hello,

    how can I delete the standard title? I like to use the textfield I created to become the wordpress standard title.

    Any help?

    Cheers,
    Denis

  • This take a couple of steps, first hiding the title WP title field. I see that you’re also not using the WP editor so that makes it a bit easier

    
    add_action('admin_head', 'hide_wp_title_input');
    function hide_wp_title_input() {
      $screen = get_current_screen();
      if ($screen->id != 'your-custom-post-type') {
        return;
      }
      ?>
        <style type="text/css">
          #post-body-content {
            display: none;
          }
        </style>
      <?php 
    }
    

    next, update the post to use the custom title field for the post title

    
    // you'll want to rename the function
    add_action('acf/save_post', 'save_post_type_post'), 20); // fires after ACF
    function save_post_type_post($post_id) {
      $post_type = get_post_type($post_id);
      if ($post_type != 'your-custom-post_type') {
        return;
      }
      $post_title = get_field('your_custom_field', $post_id);
      $post_name = sanitize_title($post_title);
      $post = array(
        'ID' => $post_id,
        'post_name' => $post_name,
        'post_title' => $post_title
      );
      wp_update_post($post);
    }
    
  • @hube2

    Hello John,

    this works!

    One ) is too much.

    add_action('acf/save_post', 'save_post_type_post'), 20);

    Thanks a lot.
    Denis

  • hi,
    can you pls help me to understand where to add this code? Inside the function.php file of my template or where?
    Thanks for your help

  • It would go in functions.php

  • I tried hiding the title using the first block of code and ended up hiding the entire WP editor.

    This:

        <style type="text/css">
          #post-body-content {
            display: none;
          }
        </style>

    Should be replaced by:

        <style type="text/css">
          #titlediv {
            display: none;
          }
        </style>

    if the aim is hiding only the “title” field of the editor.

    Glad I found this.

  • I have more than one custom field, so code looks like this:

    // Get title from acf into post title
    add_action('acf/save_post', 'upd_cust_post_title', 20); // fires after ACF
    function upd_cust_post_title($post_id) {
      $post_type = get_post_type($post_id);
      if ($post_type != 'house') {
        return;
      }
      $post_title = get_field('name', $post_id);
      $post_name = sanitize_title($post_title);
      $post = array(
        'ID' => $post_id,
        'post_name' => $post_name,
        'post_title' => $post_title
      );
      wp_update_post($post);
    }
    
    add_action('acf/save_post', 'upd_car_post_title', 20); // fires after ACF
    
    function upd_ann_post_title($post_id) {
      $post_type = get_post_type($post_id);
      if ($post_type != 'car') {
        return;
      }
      $post_title = get_field('title', $post_id);
      $post_name = sanitize_title($post_title);
      $post = array(
        'ID' => $post_id,
        'post_name' => $post_name,
        'post_title' => $post_title
      );
      wp_update_post($post);
    }
    
    

    Is that’s the way to do it? Or there’s better way?

    Thanks

  • So I just keep adding new functions with every new custom post type?

  • You can do it in one function, you just have to check for each post type. Rather than returning

    
    if ($post_type == 'car') {
      // do the car post type
    }
    if ($post_type == 'house') {
      // do the house post type
    }
    

    If you used the same field name for the acf field on both of these post types then you could do something like

    
    if ($post_type == 'car' || $post_type == 'house') {
      // do the house and car stuff
    }
    
  • Great, this is the better way inside one function.
    Thanks you, really appreciate.

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

The topic ‘Delete WP Title and use ACF Title’ is closed to new replies.