I've setup a custom post type with a series of custom fields using the WPAlchemy class. I'm trying to take the value of one of the custom fields and use that as the post title. So far, though, I've had no success. I've been browsing around and I've tried the following two different blocks of code:
function custom_post_type_title ( $post_id ) {
global $wpdb;
if ( get_post_type( $post_id ) == 'listing' ) {
$title = get_post_meta($post_id, 'listing_name', true);
$where = array( 'ID' => $post_id );
$wpdb->update( $wpdb->posts, array( 'post_title' => $title ), $where );
}
}
add_action('init', 'listing_save_post');
function listing_save_post( $post_id ) {
if ( ! defined( 'DOING_AUTOSAVE' ) && ! DOING_AUTOSAVE ) return;
add_action('save_post', 'custom_post_type_title', 100);
add_action('publish_post', 'custom_post_type_title', 100);
}
And.. (temporarily commenting out the nonce fields until I figure out how they work)
add_filter('wp_insert_post_data', 'change_title', 99, 2);
function change_title($data, $postarr)
{
// If it is our form has not been submitted, so we dont want to do anything
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
// Verify this came from the our screen and with proper authorization because save_post can be triggered at other times
// if(!isset($_POST['my_nonce_field'])) return;
// If nonce is set, verify it
// if(isset($_POST['my_nonce_field']) && !wp_verify_nonce($_POST['my_nonce_field'], plugins_url(__FILE__))) return;
// Combine address with term
$title = $_POST['listing_name'];
$data['post_title'] = $title;
return $data;
}
In both cases, the results are the same:
On saving the new post, it reloads the page, says it worked, and all of the custom fields are empty. (Without either code set running, the custom post works just fine and the fields are saved).
On viewing the custom post type index, the posts aren't there.
Any ideas?
UPDATE
To clarify, I am using a custom post without the title support. I am trying to populate the title field nonetheless, though, using the value in the custom field.