###前提・実現したいこと
人気記事を表示させるために下記サイトを参考に実装しました。
下記サイトの通りに実装すると、作成したカスタムフィールドの値を毎月1日にリセットされます。
こちら毎月1日にリセットではなく、15日毎にリセットにし、人気記事を表示させたいですが、
どうすればよいかわからず困ってます。
お力添えいただきますよう、よろしくお願いいたします。
function.php
php
1function wpb_set_post_views($postID) { 2 $count_key = 'wpb_post_views_count'; 3 $count = get_post_meta($postID, $count_key, true); 4 if($count==''){ 5 $count = 0; 6 delete_post_meta($postID, $count_key); 7 add_post_meta($postID, $count_key, '0'); 8 }else{ 9 $count++; 10 update_post_meta($postID, $count_key, $count); 11 } 12} 13//To keep the count accurate, lets get rid of prefetching 14remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 15 16/* add post views to single page */ 17function wpb_track_post_views ($post_id) { 18 if ( !is_single() ) return; 19 if ( empty ( $post_id) ) { 20 global $post; 21 $post_id = $post->ID; 22 } 23 wpb_set_post_views($post_id); 24} 25add_action( 'wp_head', 'wpb_track_post_views'); 26 27add_action('my_hourly_event', 'my_hourly_action'); 28 29function my_hourly_action() { 30 // 投稿記事全取得 31 $args = array( 32 'posts_per_page' => -1, 33 'post_type' => array( 34 'post' 35 ) 36 ); 37 // 変数に格納 38 $the_query = new WP_Query($args); 39 $count_key = 'wpb_post_views_count'; 40 delete_post_meta_by_key('wpb_post_views_count'); 41 if ($the_query->have_posts()) : 42 while ($the_query->have_posts()) : $the_query->the_post(); 43 $post_id = $the_query->post->ID; 44 $count = get_post_meta($post_id, $count_key, true); 45 if(empty($count)){ 46 $count = 0; 47 delete_post_meta($post_id, $count_key); 48 add_post_meta($post_id, $count_key, '0'); 49 }else{ 50 $count++; 51 update_post_meta($post_id, $count_key, 0); 52 } 53 endwhile; 54 endif; 55} 56 57// イベントの時間追加 58 59add_filter('cron_schedules', 'my_interval' ); 60function my_interval($schedules) { 61 date_default_timezone_set( 'Asia/Tokyo' ); 62 $dt = new DateTime('now'); 63 $dt_2 = new DateTime('midnight first day of next month'); 64 $d = $dt_2->diff($dt, true); 65 $dt_array = get_object_vars($d); 66 $day = $dt_array["d"] * 24 * 60 * 60; 67 $hour = $dt_array["h"] * 60 * 60; 68 $minutes = $dt_array["i"] * 60; 69 $second = $dt_array["s"]; 70 $difftime = $day + $hour + $minutes + $second + 60; 71 $schedules['Nextmonth'] = array( 72 'interval' => $difftime, 73 'display' => 'Nextmonth' 74 ); 75 return $schedules; 76} 77 78function my_activation() { 79 // イベントが未登録なら登録する 80 if(!wp_next_scheduled('my_hourly_event')){ 81 wp_schedule_event(time(), 'Nextmonth', 'my_hourly_event'); 82 } 83} 84add_action('wp', 'my_activation'); 85 86// イベント排除 87register_deactivation_hook(__FILE__, 'my_deactivation'); 88function my_deactivation() { 89 wp_clear_scheduled_hook('my_hourly_event'); 90}
index.php
php
1<?php 2$popularpost = new WP_Query( array( 'posts_per_page' => 10, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); 3 4while ( $popularpost->have_posts() ) : $popularpost->the_post();?> 5 <div class="latest_wrap"> 6 <a href="<?php the_permalink(); ?>"> 7 <?php if ( has_post_thumbnail() ) { ?> 8 <?php the_post_thumbnail('thumbnail', array('class' => 'thumbnail')); ?> 9 <?php } else { ?> 10 <img src="<?php echo get_stylesheet_directory_uri(); ?>/img/no_image3.gif" /> 11 <?php } ?> 12 13 <h2 class="title"><?php the_title(); ?></h2> 14 <span class="view_count"><?php echo post_custom('wpb_post_views_count'); ?></span> 15 </a> 16 </div> 17<?php endwhile; ?>
回答1件
あなたの回答
tips
プレビュー