WordPress.org

Ready to get started?Download WordPress

Forums

Saving custom post title built from custom fields (4 posts)

  1. bradickson
    Member
    Posted 2 years ago #

    Hi.
    I've spent many hours experimenting with filters and actions trying to achieve what I expected to be a simple task.

    All I want to do is have the post_title, when saved in the db, defined by custom fields in the post, as I've hidden the title input box.

    I've tried:

    add_filter('title_save_pre','my_function');
    add_action('save_post','my_function');
    add_action('insert_post','my_function');

    all with no success.

    The only thing I've found that works somewhat was when I tried:

    add_filter('title_save_pre','my_function');
    function my_function($title) {
         $title_mod = $title(with some string modification done to it)
         return $title_mod;
    }

    But this isn't enough as I can't succeed in finding and using values of the post's custom fields properly, with which I need to build the post's title for it to be saved in the db.

    Can someone please suggest the best solution?
    Thanks, I appreciate help anyone can give.

  2. Eric
    Member
    Posted 2 years ago #

    I'm trying to do the same thing. Any luck, yet?

  3. bradickson
    Member
    Posted 1 year ago #

    It's buried in code I haven't looked at in weeks, but I think my solution was:

    1. Use the 'Title Format' plugin, which adds a filter to the title so that every time it displays, it replaces the title your custom title. (Set with the plugin settings) This provides an easy way to use your custom fields to create the title, but if you want to replace permanently...
    2. Using this replaced title, then run an UPDATE query to change the title in the database directly, so that it becomes the actual title in the database and not just a filter whenever you view the title.
    3. This won't insert your desired title into the db on publish, but will replace the default undefined title the first time you refresh the list of posts.

      Hope that makes sense!

  4. jgtjones
    Member
    Posted 1 year ago #

    I am new to WordPress and have a similar need. I have found various posts, including one described as "resolved" one year ago. Essentially your approach was correct, but 'title_save_pre' has been replaced by {$field_no_prefix}_save_pre which simply obscures what is going on. These guys writing WordPress modules should use plain English to help us mortals.
    Presumably the code goes into functions.php. Including:`
    add_filter('{$field_no_reply}_save_pre','set_custom_title');
    is not going to set any post title. In my case I want to set the post title to names entered in custom post fields. These can presumably be referred to in the following way:

    function set_custom_title(){
    if ($post->post_type == "my_custom_post_type"){
    $post->post_title = $_POST['first_name'] . " " . $_POST['last_name'];
    }
    }

    I say presumably. I shall check it out, but if anyone knows, please say.

Topic Closed

This topic has been closed to new replies.

About this Topic