WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Generating and setting a custom post title (6 posts)

  1. fatalx
    Member
    Posted 3 years ago #

    Hello,

    I am working with custom posts types and I would like to generate a custom post title using a combination of my custom fields. That way the user doesn't have to ever enter a title themselves.

    Currently I have been using this.

    add_filter('title_save_pre','my_function');

    with "my_function" grabbing my custom fields and returning a title. That works fine except it only works when I update a post. It doesn't set the title when I originally create and save the post. How can I fix that? What is the proper method to do this.

    Thank You.

  2. fatalx
    Member
    Posted 3 years ago #

    I also looked into using

    wp_update_post

    but since I'm using a hook I it causes the browser to timeout.

    Any thoughts?

  3. fatalx
    Member
    Posted 3 years ago #

    Well I fixed my problem. In the end

    add_filter('title_save_pre','my_function');

    worked perfectly fine. The problem was that my function was calling the custom fields from

    global $post

    and when the post is first published those custom fields have not been written to $post yet.

    So you have to use

    $_POST['your custom field']

    to properly grab your post title.

    Hope this helps someone.

  4. C Krueger
    Member
    Posted 3 years ago #

    This method caused problems for me in WordPress 3.1, but its possible that I didn't handle the 'title_save_pre' filter correctly.

    I've set up a custom post type "Testimonial", and I want the post title to be defined by the custom "Client" meta field, and I don't want the title editor to be available.

    In the end, I want $post->post_title == get_post_custom($post->ID)["client"][0]

    Could you show me what the 'my_function' method should look like when you bind it to the 'title_save_pre' filter?

    Thanks for the post - its been hard to find other information on this topic.

  5. fatalx
    Member
    Posted 2 years ago #

    Response 3 months later haha sry but for anyone coming across this again the 'my_function' function is really simple all it has to do is return a value. The returned value will be the title.

    So if I wanted to grab some meta data that the user entered and make that my title it would look like this.

    function my_function(){
            $meta_data = $_POST['meta_name'];
    	return $meta_data;
    }

    You can perform any operation you wish on the title inside that function.

  6. shahz
    Member
    Posted 2 years ago #

    Hey fatalx,

    How did you alter the title for the custom post type? If we were not using any meta content. I have this snippet which is currently not working for me. Mind helping please?

    add_filter('the_title', 'new_title');
    function new_title($title) {
    	global $post, $post_ID;
    	$title['custom_version'] = 'Application Name v'.$title;
    	return $title;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic