###前提・実現したいこと
サイドバーにランキング表示させるために下記のサイトを参考しています。
開発環境では問題なく表示できていたのですが、本番環境に反映されずに困っています。
wpb_set_post_views関数でカスタムフィールドを追加しているはずなのに、本番環境ではカスタムフィールドが追加できていませんでした。
お手数をおかけしますが、原因追究にお力添えいただければ幸いです。
###sideber.php
<h2>ランキング</h2> <div> <ul class="ranking-list"> <?php $popularpost = new WP_Query(array('posts_per_page' => 5, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC')); ?> <?php $i=1; while ( $popularpost->have_posts() ) : $popularpost->the_post();?> <li> <a href="<?php the_permalink(); ?>"> <span class="num"><?php echo $i; ?></span> <?php the_title(); ?> </a> </li> <?php $i++; endwhile; ?> </ul> </div>
###function.php
function wpb_set_post_views($postID) { $count_key = 'wpb_post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); }else{ $count++; update_post_meta($postID, $count_key, $count); } } remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); function wpb_track_post_views ($post_id) { if ( !is_single() ) return; if ( empty ( $post_id) ) { global $post; $post_id = $post->ID; } wpb_set_post_views($post_id); } add_action( 'wp_head', 'wpb_track_post_views'); add_action('my_hourly_event', 'my_hourly_action'); function my_hourly_action() { $args = array( 'posts_per_page' => -1, 'post_type' => array( 'post' ) ); $the_query = new WP_Query($args); $count_key = 'wpb_post_views_count'; delete_post_meta_by_key('wpb_post_views_count'); if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); $post_id = $the_query->post->ID; $count = get_post_meta($post_id, $count_key, true); if(empty($count)){ $count = 0; delete_post_meta($post_id, $count_key); add_post_meta($post_id, $count_key, '0'); }else{ $count++; update_post_meta($post_id, $count_key, 0); } endwhile; endif; } add_filter('cron_schedules', 'my_interval' ); function my_interval($schedules) { date_default_timezone_set( 'Asia/Tokyo' ); $dt = new DateTime('now'); $dt_2 = new DateTime('midnight first day of next month'); $d = $dt_2->diff($dt, true); $dt_array = get_object_vars($d); $day = $dt_array["d"] * 24 * 60 * 60; $hour = $dt_array["h"] * 60 * 60; $minutes = $dt_array["i"] * 60; $second = $dt_array["s"]; $difftime = $day + $hour + $minutes + $second + 60; $schedules['Nextmonth'] = array( 'interval' => $difftime, 'display' => 'Nextmonth' ); return $schedules; } function my_activation() { if(!wp_next_scheduled('my_hourly_event')){ wp_schedule_event(time(), 'Nextmonth', 'my_hourly_event'); } } add_action('wp', 'my_activation'); register_deactivation_hook(__FILE__, 'my_deactivation'); function my_deactivation() { wp_clear_scheduled_hook('my_hourly_event'); }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/08/18 09:59
退会済みユーザー
2017/08/18 10:44
2018/07/17 02:43