WordPressの投稿記事は、誤字脱字を少し修正しただけでも更新日時が変更されてしまう。
誤字の訂正だけで3年前の記事の更新日が更新されてしまうのはおかしい。
編集画面に更新日時の変更オプション追加
それで、記事の内容に大きな変更のない場合は更新日時を更新しないようにする。
functions.php に次を追加する。
?><?php
//---------------------------------------------------------------------------
// 記事投稿(編集)画面に更新レベルのボックス追加
//---------------------------------------------------------------------------
/* ボックス追加 */
if( function_exists( 'thk_post_update_level' ) === false ):
function thk_post_update_level() {
add_meta_box( 'update_level', '更新方法', 'post_update_level_box', 'post', 'side', 'default' );
add_meta_box( 'update_level', '更新方法', 'post_update_level_box', 'page', 'side', 'default' );
}
add_action( 'admin_menu', 'thk_post_update_level' );
endif;
/* メインフォーム */
if( function_exists( 'post_update_level_box' ) === false ):
function post_update_level_box() {
global $post;
?>
<div style="padding-top: 5px; overflow: hidden;">
<div style="padding:5px 0"><input name="update_level" type="radio" value="high" />通常更新</div>
<div style="padding: 5px 0"><input name="update_level" type="radio" value="low" checked="checked" />修正のみ(更新日時を変更せず記事更新)</div>
<div style="padding: 5px 0"><input name="update_level" type="radio" value="del" />更新日時消去(公開日時と同じにする)</div>
<div style="padding: 5px 0; margin-bottom: 10px"><input id="update_level_edit" name="update_level" type="radio" value="edit" />更新日時を手動で変更</div>
<?php
if( get_the_modified_date( 'c' ) ) {
$stamp = '更新日時: <span style="font-weight:bold">' . get_the_modified_date( __( 'M j, Y @ H:i' ) ) . '</span>';
}
else {
$stamp = '更新日時: <span style="font-weight:bold">未更新</span>';
}
$date = date_i18n( get_option('date_format') . ' @ ' . get_option('time_format'), strtotime( $post->post_modified ) );
?>
<style>
.modtime { padding: 2px 0 1px 0; display: inline !important; height: auto !important; }
#timestamp_mod_div { padding-top: 5px; line-height: 23px; }
#timestamp_mod_div p { margin: 8px 0 6px; }
#timestamp_mod_div input { border-width: 1px; border-style: solid; }
#timestamp_mod_div select { height: 21px; line-height: 14px; padding: 0; vertical-align: top;font-size: 12px; }
#aa_mod, #jj_mod, #hh_mod, #mn_mod { padding: 1px; font-size: 12px; }
#jj_mod, #hh_mod, #mn_mod { width: 2em; }
#aa_mod { width: 3.4em; }
</style>
<span class="modtime"><?php printf( $stamp, $date ); ?></span>
<div id="timestamp_mod_div" onkeydown="document.getElementById('update_level_edit').checked=true" onclick="document.getElementById('update_level_edit').checked=true">
<?php thk_time_mod_form(); ?>
</div>
</div>
<?php
}
endif;
/* 更新日時変更の入力フォーム */
if( function_exists( 'thk_time_mod_form' ) === false ):
function thk_time_mod_form() {
global $wp_locale, $post;
$tab_index = 0;
$tab_index_attribute = '';
if ( (int) $tab_index > 0 ) {
$tab_index_attribute = ' tabindex="' . $tab_index . '"';
}
$jj_mod = mysql2date( 'd', $post->post_modified, false );
$mm_mod = mysql2date( 'm', $post->post_modified, false );
$aa_mod = mysql2date( 'Y', $post->post_modified, false );
$hh_mod = mysql2date( 'H', $post->post_modified, false );
$mn_mod = mysql2date( 'i', $post->post_modified, false );
$ss_mod = mysql2date( 's', $post->post_modified, false );
$year = '<label for="aa_mod" class="screen-reader-text">年' .
'</label><input type="text" id="aa_mod" name="aa_mod" value="' .
$aa_mod . '" size="4" maxlength="4"' . $tab_index_attribute . ' autocomplete="off" />年';
$month = '<label for="mm_mod" class="screen-reader-text">月' .
'</label><select id="mm_mod" name="mm_mod"' . $tab_index_attribute . ">n";
for( $i = 1; $i < 13; $i = $i +1 ) {
$monthnum = zeroise($i, 2);
$month .= "ttt" . '<option value="' . $monthnum . '" ' . selected( $monthnum, $mm_mod, false ) . '>';
$month .= $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
$month .= "</option>n";
}
$month .= '</select>';
$day = '<label for="jj_mod" class="screen-reader-text">日' .
'</label><input type="text" id="jj_mod" name="jj_mod" value="' .
$jj_mod . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />日';
$hour = '<label for="hh_mod" class="screen-reader-text">時' .
'</label><input type="text" id="hh_mod" name="hh_mod" value="' . $hh_mod .
'" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
$minute = '<label for="mn_mod" class="screen-reader-text">分' .
'</label><input type="text" id="mn_mod" name="mn_mod" value="' . $mn_mod .
'" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
printf( '%1$s %2$s %3$s @ %4$s : %5$s', $year, $month, $day, $hour, $minute );
echo '<input type="hidden" id="ss_mod" name="ss_mod" value="' . $ss_mod . '" />';
}
endif;
/* 「修正のみ」は更新しない。それ以外は、それぞれの更新日時に変更する */
if( function_exists( 'thk_insert_post_data' ) === false ):
function thk_insert_post_data( $data, $postarr ){
$mydata = isset( $_POST['update_level'] ) ? $_POST['update_level'] : null;
if( $mydata === 'low' ){
unset( $data['post_modified'] );
unset( $data['post_modified_gmt'] );
}
elseif( $mydata === 'edit' ) {
$aa_mod = $_POST['aa_mod'] <= 0 ? date('Y') : $_POST['aa_mod'];
$mm_mod = $_POST['mm_mod'] <= 0 ? date('n') : $_POST['mm_mod'];
$jj_mod = $_POST['jj_mod'] > 31 ? 31 : $_POST['jj_mod'];
$jj_mod = $jj_mod <= 0 ? date('j') : $jj_mod;
$hh_mod = $_POST['hh_mod'] > 23 ? $_POST['hh_mod'] -24 : $_POST['hh_mod'];
$mn_mod = $_POST['mn_mod'] > 59 ? $_POST['mn_mod'] -60 : $_POST['mn_mod'];
$ss_mod = $_POST['ss_mod'] > 59 ? $_POST['ss_mod'] -60 : $_POST['ss_mod'];
$modified_date = sprintf( 'd-d-d d:d:d', $aa_mod, $mm_mod, $jj_mod, $hh_mod, $mn_mod, $ss_mod );
if ( ! wp_checkdate( $mm_mod, $jj_mod, $aa_mod, $modified_date ) ) {
unset( $data['post_modified'] );
unset( $data['post_modified_gmt'] );
return $data;
}
$data['post_modified'] = $modified_date;
$data['post_modified_gmt'] = get_gmt_from_date( $modified_date );
}
elseif( $mydata === 'del' ) {
$data['post_modified'] = $data['post_date'];
$data['post_modified_gmt'] = $data['post_date_gmt'];
}
return $data;
}
add_filter( 'wp_insert_post_data', 'thk_insert_post_data', 10, 2 );
endif;
参考:
これで、更新日時を変更するか否かを選択できるようになる。
全記事の更新日時をリセット
これまでに、不適切な更新日時の変更が何度も繰り返されてきたので、全ての記事の更新日時をリセット、初期化し、公開日時と同じに戻す。
上記の機能でも個別記事は編集画面から更新日時をリセットすることは可能であるが、一括で全記事の更新日時を初期化したい。
phpmyadmin などで、次のSQLコマンドを実行する。
UPDATE wp_posts SET post_modified = post_date , post_modified_gmt = post_date_gmt
これで、全記事の更新日時が公開日時と同じになった。
次のようにすれば、全記事の更新日時を1年間ずらしたりすることもできる。
UPDATE wp_posts
SET post_date = DATE_ADD( post_date, INTERVAL 12 MONTH ), post_date_gmt = DATE_ADD( post_date_gmt, INTERVAL 12 MONTH ), post_modified = DATE_ADD( post_modified, INTERVAL 12 MONTH ), post_modified_gmt = DATE_ADD( post_modified_gmt, INTERVAL 12 MONTH )
参考:
公開日と更新日時の併記と相対日時表示
記事が更新されていない場合は、記事の公開日のみを表示、更新されている場合のみ、公開日と更新日を併記する。
同時に、何日前、何年前という相対日時も表示する。
Pjax-blog テーマでは、
class-leapin-single-tags.php (inc/single/class-leapin-single-tags.php
の40行目付近、function get_header_meta_tag() を次のように編集する。
public static function get_header_meta_tag()
{
$hide_modified_date = leapin_mod_sr_only(LEAPIN_IDs::$leapin_hide_modified_date_below_title);
$hide_category = leapin_mod_sr_only(LEAPIN_IDs::$leapin_hide_category_below_title);
$rtn = '<div class="post-metas">';
if(get_the_time('Y/m/d') != get_the_modified_date('Y/m/d')){
$archive_year = get_the_time( 'Y' );
$archive_month = get_the_time( 'm' );
$rtn .= sprintf('<i class="far fa-calendar-alt updated-icon"></i> %1$s',
/* the time the post was published */
'<a href="' . get_month_link( $archive_year, $archive_month ) . ' " class="wpp-date"><time class="published entry-time" datetime="' . get_the_time('c') . '" itemprop="datePublished">' . get_the_time(get_option('date_format')) . preg_replace( '@曜日@', '', get_the_date( '-l G:i' )). ' 【' . human_time_diff(get_the_time('U'),current_time('timestamp')) . '前】</time></a>'
);
$archive_year = get_the_modified_date( 'Y' );
$archive_month = get_the_modified_date( 'm' );
$rtn .= sprintf('<i class="fas fa-redo-alt updated-icon"></i> %1$s',
/* the time the post was published */
'<a href="' . get_month_link( $archive_year, $archive_month ) . ' " class="wpp-date"><time class="updated entry-time" datetime="' . get_the_modified_date('c') . '" itemprop="dateModified">' . get_the_modified_date(get_option('date_format')) . ' 【' . human_time_diff(get_the_modified_date('U'),current_time('timestamp')) . '前】</time></a>'
);
}else{
$archive_year = get_the_time( 'Y' );
$archive_month = get_the_time( 'm' );
$rtn .= sprintf('<i class="far fa-calendar-alt updated-icon"></i> %1$s',
/* the time the post was published */
'<a href="' . get_month_link( $archive_year, $archive_month ) . ' " class="wpp-date"><time class="published entry-time" datetime="' . get_the_time('c') . '" itemprop="datePublished">' . get_the_time(get_option('date_format')) . preg_replace( '@曜日@', '', get_the_date( '-l G:i' )). ' 【' . human_time_diff(get_the_time('U'),current_time('timestamp')) . '前】</time></a>'
);
}
$rtn .= sprintf('<i class="far fa-folder-open category-icon' . $hide_category . '"></i> %1$s',
'<span class="entry-category' . $hide_category . '" itemprop="">' . LEAPIN_Single_Tags::get_header_category_tag() . '</span>'
);
return $rtn . '</div>';
}
functions.php に次を追加。
add_filter( 'human_time_diff', 'custom_human_time_diff' );
function custom_human_time_diff( $since ) {
return str_replace( 'か月', 'ヶ月', $since );
}





