我使用wordpress作为cms,所以在我的自定义帖子类型上,我不需要标题,只使用自定义字段。然而,标题仍然在使用,并且收到了“自动草稿”的值,这是我不想要的,即使我现在不使用永久链接。
最快速、最简单的解决方案是将自定义字段“ref.\\u nr.”中的值复制到标题中,因为这将是我的唯一标识符,如果我以后需要进行更改,它将起到更好的作用。
不幸的是,我不知道从哪里开始。如果您想贡献代码,自定义的post类型称为“listing”。
非常感谢您的所有意见!
我使用wordpress作为cms,所以在我的自定义帖子类型上,我不需要标题,只使用自定义字段。然而,标题仍然在使用,并且收到了“自动草稿”的值,这是我不想要的,即使我现在不使用永久链接。
最快速、最简单的解决方案是将自定义字段“ref.\\u nr.”中的值复制到标题中,因为这将是我的唯一标识符,如果我以后需要进行更改,它将起到更好的作用。
不幸的是,我不知道从哪里开始。如果您想贡献代码,自定义的post类型称为“listing”。
非常感谢您的所有意见!
Try this:
add_action(\'init\', \'replace_title_logic\');
function replace_title_logic() {
add_filter(\'wp_insert_post_data\', \'replace_title_before_update\', 20, 2);
add_action(\'wp_insert_post\', \'replace_title_after_save\', 20, 2);
add_filter(\'wp_insert_post_empty_content\', \'allow_empty_content_for_listing\', 20 , 2);
}
function replace_title_before_update( $data, $postarr ) {
if ( is_int($postarr[\'ID\']) && ($data[\'post_type\'] == \'listing\') ) {
$custom = get_post_meta($postarr[\'ID\'], \'ref._nr.\', true );
if ( $custom && ( $custom != $data[\'post_title\'] ) ) $data[\'post_title\'] = $custom;
}
return $data;
}
function replace_title_after_save( $post_ID, $post ) {
if ( $post->post_type != \'listing\' ) return;
$custom = get_post_meta($post_ID, \'ref._nr.\', true );
if ( $custom && ( $custom != $post->post_title ) && $post->post_status != \'trash\') {
remove_action(\'wp_insert_post\', \'replace_title_after_save\', 20, 2);
remove_filter(\'wp_insert_post_data\', \'replace_title_before_update\', 20, 2);
$postarr = ( array(\'ID\' => $post_ID, \'post_title\' => $custom) );
wp_insert_post($postarr);
}
}
function allow_empty_content_for_listing( $maybe_empty, $postarr ) {
if ( $postarr[\'post_type\'] == \'listing\' ) return false;
}
我使用wordpress作为cms,所以在我的自定义帖子类型上,我不需要标题,只使用自定义字段。然而,标题仍然在使用,并且收到了“自动草稿”的值,这是我不想要的,即使我现在不使用永久链接。
最快速、最简单的解决方案是将自定义字段“ref.\\u nr.”中的值复制到标题中,因为这将是我的唯一标识符,如果我以后需要进行更改,它将起到更好的作用。
不幸的是,我不知道从哪里开始。如果您想贡献代码,自定义的post类型称为“listing”。
非常感谢您的所有意见!
Try this:
add_action(\'init\', \'replace_title_logic\');
function replace_title_logic() {
add_filter(\'wp_insert_post_data\', \'replace_title_before_update\', 20, 2);
add_action(\'wp_insert_post\', \'replace_title_after_save\', 20, 2);
add_filter(\'wp_insert_post_empty_content\', \'allow_empty_content_for_listing\', 20 , 2);
}
function replace_title_before_update( $data, $postarr ) {
if ( is_int($postarr[\'ID\']) && ($data[\'post_type\'] == \'listing\') ) {
$custom = get_post_meta($postarr[\'ID\'], \'ref._nr.\', true );
if ( $custom && ( $custom != $data[\'post_title\'] ) ) $data[\'post_title\'] = $custom;
}
return $data;
}
function replace_title_after_save( $post_ID, $post ) {
if ( $post->post_type != \'listing\' ) return;
$custom = get_post_meta($post_ID, \'ref._nr.\', true );
if ( $custom && ( $custom != $post->post_title ) && $post->post_status != \'trash\') {
remove_action(\'wp_insert_post\', \'replace_title_after_save\', 20, 2);
remove_filter(\'wp_insert_post_data\', \'replace_title_before_update\', 20, 2);
$postarr = ( array(\'ID\' => $post_ID, \'post_title\' => $custom) );
wp_insert_post($postarr);
}
}
function allow_empty_content_for_listing( $maybe_empty, $postarr ) {
if ( $postarr[\'post_type\'] == \'listing\' ) return false;
}