$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Using save_post to replace the post's title|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Using save_post to replace the post's title

matteradmin9PV0评论

I am using custom posts, and in these, I don't a need for the title.

This causes Wordpress to set the titles of my posts to "Auto Draft".

I'd like to change the title's value to something else, computed from other fields in my post.

How do I go about doing that using save_post or some other means?

I am using custom posts, and in these, I don't a need for the title.

This causes Wordpress to set the titles of my posts to "Auto Draft".

I'd like to change the title's value to something else, computed from other fields in my post.

How do I go about doing that using save_post or some other means?

Share Improve this question asked Nov 21, 2012 at 18:46 Tsahi Levent-LeviTsahi Levent-Levi 3851 gold badge3 silver badges7 bronze badges 5
  • Please edit your question to include your register_post_type() call. – Chip Bennett Commented Nov 21, 2012 at 18:54
  • What are you trying to achieve exactly? You don't want a post title for your CPT at all or you want it to be set from a custom field value? – Rutwick Gangurde Commented Nov 21, 2012 at 19:01
  • 2 I don't want it at all, but in the posts list I can't remove it as I won't be able to edit posts anymore. This means that I need a way to place a "fake" title of some kind instead. – Tsahi Levent-Levi Commented Nov 21, 2012 at 19:28
  • 1 So, what you really want to do is to modify the manage posts screen, to output different columns for your custom post type? If so, that might be a more beneficial question to ask. :) – Chip Bennett Commented Nov 21, 2012 at 19:38
  • 1 It is jsut a part. One asks this kind of "impact" questions because he seeks an answer to help him orginize many aspects of a project. Search ,templating e.t.c – e4rthdog Commented Feb 14, 2016 at 10:36
Add a comment  | 

4 Answers 4

Reset to default 24

This simplest method would be to edit the data at the point it's inserted, rather than updating it afterwards, using wp_insert_post_data instead of save_post. This works on creating a new post or updating an existing post without change. It also avoids the danger of creating an infinite loop by triggering update_post within save_post.

add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 1 ); // Grabs the inserted post data so you can modify it.

function modify_post_title( $data )
{
  if($data['post_type'] == 'rating' && isset($_POST['rating_date'])) { // If the actual field name of the rating date is different, you'll have to update this.
    $date = date('l, d.m.Y', strtotime($_POST['rating_date']));
    $title = 'TV ratings for ' . $date;
    $data['post_title'] =  $title ; //Updates the post title to your new title.
  }
  return $data; // Returns the modified data.
}

I had the exact same need, so I wrote this function - which works. Modify it to your needs. Hope this helps.

// set daily rating title
function set_rating_title ($post_id) {
    if ( $post_id == null || empty($_POST) )
        return;

    if ( !isset( $_POST['post_type'] ) || $_POST['post_type']!='rating' )  
        return; 

    if ( wp_is_post_revision( $post_id ) )
        $post_id = wp_is_post_revision( $post_id );

    global $post;  
    if ( empty( $post ) )
        $post = get_post($post_id);

    if ($_POST['rating_date']!='') {
        global $wpdb;
        $date = date('l, d.m.Y', strtotime($_POST['rating_date']));
        $title = 'TV ratings for ' . $date;
        $where = array( 'ID' => $post_id );
        $wpdb->update( $wpdb->posts, array( 'post_title' => $title ), $where );
    }
}
add_action('save_post', 'set_rating_title', 12 );

Here's a solution that uses a static variable to prevent an infinite loop. This allows you to safely call wp_update_post() inside of a function that is hooked to save_post.

function km_set_title_on_save( $post_id ) {

    // Set this variable to false initially.
    static $updated = false;

    // If title has already been set once, bail.
    if ( $updated ) {
        return;
    }

    // Since we're updating this post's title, set this
    // variable to true to ensure it doesn't happen again.
    $updated = true;

    $date           = get_post_meta( $post_id, 'rating_date', true );
    $date_formatted = date( 'l, d.m.Y', strtotime( $date ) );

    // Update the post's title.
    wp_update_post( [
        'ID'         => $post_id,
        'post_title' => 'TV ratings for ' . $date_formatted,
    ] );
}
add_action( 'save_post', 'km_set_title_on_save' );

Note: To limit this functionality to a certain post type, use the save_post_{$post->post_type} hook instead of save_post.

Try the filter default_title:

add_filter( 'default_title', 'my_default_title', 10, 2 );

function my_default_title( $post_title, $post ){

  $custom_post_type = 'my_awesome_cpt';

  // do it only on your custom post type(s)
  if( $post->post_type !== $custom_post_type )
    return $post_title;

  // create your preferred title here
  $post_title = $custom_post_type . date( 'Y-m-d :: H:i:s', time() );

  return $post_title;
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far