Skip to content

Instantly share code, notes, and snippets.

@gatespace
Created March 22, 2019 08:20
Show Gist options
  • Save gatespace/83c5060fce3616fd74ea3dc1ee4205bf to your computer and use it in GitHub Desktop.
Save gatespace/83c5060fce3616fd74ea3dc1ee4205bf to your computer and use it in GitHub Desktop.
[WordPress] 初回の記事公開日をカスタムフィールドに残す via. https://qiita.com/gatespace/items/a8853c96f892426bfabe
add_filter( 'manage_posts_columns' , 'add_publish_date_columns' );
function add_publish_date_columns( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_display_name ) {
if ( $column_name == 'date' ) {
$new_columns['publish_date'] = '掲載日時';
}
$new_columns[ $column_name ] = $column_display_name;
}
return $new_columns;
}
add_action( 'manage_posts_custom_column' , 'custom_publish_date_column', 10, 2 );
function custom_publish_date_column( $column, $post_id ) {
switch ( $column ) {
case 'publish_date' :
if ( ! empty( get_post_meta( $post_id, '_publish_date', true ) ) ) {
echo date_i18n( get_option('date_format') . get_option('time_format'), get_post_meta( $post_id, '_publish_date', true ) );
}
break;
}
}
add_action( 'save_post', 'my_save_post' );
function my_save_post( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( wp_doing_cron() ) {
return;
}
if ( get_post_status ( $post_id ) == 'publish' || get_post_status ( $post_id ) == 'future' || get_post_status ( $post_id ) == 'private' ) {
if ( empty( get_post_meta( $post_id, '_publish_date', true ) ) ) {
$publish_date_u = get_the_date( 'U', $post_id );
update_post_meta( $post_id, '_publish_date', $publish_date_u );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment