Using the WordPress plugin, TDO Mini Forms, I needed to work out how to create a Post Title by using Custom Fields that the site contributors are able to enter data into.
Not being a programming genius, I went round and round in circles until today, when I spent a few hours slowly going through it all again.
The following code, when put into an “Append to Post Content” widget, takes the values from “Custom Fields 1, 2 and 3, and creates a Post Title, with all three fields joined together.
<?php
$name = get_post_meta($post_id, ‘TDOMF Form #1 Custom Field #_1’, true);
$suburb = get_post_meta($post_id, ‘TDOMF Form #1 Custom Field #_2’, true);
$phone = get_post_meta($post_id, ‘TDOMF Form #1 Custom Field #_3’, true);
$post_title .= $name . $suburb . $phone;
$postdata = array(“ID”=> $post_id, “post_title” => $post_title);
sanitize_post($postdata,”db”);
wp_update_post($postdata);
?>
eg: If Custom Field 1 = “Name”, and 2 = “Age” and 3 = “Male” the post title will be “NameAgeMale”
That didn’t quite look right, so a small change, and one line gets changed from
$post_title .= $name . $suburb . $phone;
to
$post_title .= $name .”, ” .$suburb .”, ” .$phone;
This puts a comma and a space between each field, and the result is now: “Name, Age, Male” Much more pleasing to the eye.
Putting this “Append to Post Content” widget after all other entry widgets, and I got the right result.
699.1 - 873,720
Will check this code snippet out.