我已经使用WPAlchemy类设置了一个带有一系列自定义字段的自定义帖子类型。我试图获取其中一个自定义字段的值,并将其用作文章标题。不过,到目前为止,我还没有成功。我一直在浏览,并尝试了以下两种不同的代码块:
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);
}
和。。(暂时注释nonce字段,直到我弄清楚它们是如何工作的)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;
}
在这两种情况下,结果是相同的:保存新帖子时,它会重新加载页面,并表示它正常工作,所有自定义字段均为空。(没有任何一个代码集运行,自定义贴子工作正常,字段保存)。
查看自定义帖子类型索引时,帖子不在那里。
有什么想法吗?
UPDATE
为了澄清,我使用的是一篇没有标题支持的自定义帖子。尽管如此,我还是尝试使用自定义字段中的值填充标题字段。