Виджет популярных записей за период времени
Всем привет сделал виджет популярных записей.
А вот вывод за период времени не получается, пните пожалуйста в нужном направлении.
Счетчик просмотров:
add_action('wp_head', 'brilias_postviews');
function brilias_postviews() {
setPostViews(get_the_ID());
}
function getPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count=='') {
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 просмотров";
}
return ' Просмотров: '.$count;
}
function setPostViews($postID) {
$count_key = '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);
}
}
Виджет:
<?php
if ( basename( $_SERVER['PHP_SELF'] ) == basename( __FILE__ ) ) {
die('<h1>Отказано в доступе!</h1>');
}
class Foo_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'foo_widget',
'Популярные записи',
array( 'description' => 'Вывод популярных записей', /*'classname' => 'my_widget',*/ )
);
}
/** Frontend out
* @param array $args аргументы виджета.
* @param array $instance сохраненные данные из настроек
*/
function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$param_cat = $instance['cat_id'];
$param_period = $instance['period'];
$param_quantity = $instance['quantity'];
$args = array(
'post_status' => 'publish',
'category__in' => $param_cat,
'posts_per_page' => $param_quantity,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
); ?>
<div class="top-posts">
<?php
echo $instance['title'];
$result = new WP_Query($args);
while ($result->have_posts()) { $result->the_post(); ?>
<li class="popular-posts-item">
<i class="fas fa-circle popular-posts-circle"></i>
<a href="<?php the_permalink() ?>">
<?php
$excerpt = wp_trim_words( get_the_excerpt(), 15, '...' );
echo $excerpt;
?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</div>
<?php }
// Backend options
function form( $instance ) {
$title = @ $instance['title'] ?: 'Заголовок';
$cat = @ $instance['cat_id'] ?: '1';
$period = @ $instance['period'] ?: '1';
$quantity = @ $instance['quantity'] ?: '1';
?>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Заголовок:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_html( $title ); ?>">
<label for="<?php echo $this->get_field_id( 'cat' ); ?>"><?php _e( 'ID Категории:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'cat_id' ); ?>" name="<?php echo $this->get_field_name( 'cat_id' ); ?>" type="text" value="<?php echo esc_attr( $cat ); ?>">
<label for="<?php echo $this->get_field_id( 'period' ); ?>"><?php _e( 'Период времени:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'period' ); ?>" name="<?php echo $this->get_field_name( 'period' ); ?>" type="text" value="<?php echo esc_attr( $period ); ?>">
<label for="<?php echo $this->get_field_id( 'quantity' ); ?>"><?php _e( 'Количество записей:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'quantity' ); ?>" name="<?php echo $this->get_field_name( 'quantity' ); ?>" type="text" value="<?php echo esc_attr( $quantity ); ?>">
<?php }
// Update backend options
function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? ( $new_instance['title'] ) : '';
$instance['cat_id'] = ( ! empty( $new_instance['cat_id'] ) ) ? strip_tags( $new_instance['cat_id'] ) : '';
$instance['period'] = ( ! empty( $new_instance['period'] ) ) ? strip_tags( $new_instance['period'] ) : '';
$instance['quantity'] = ( ! empty( $new_instance['quantity'] ) ) ? strip_tags( $new_instance['quantity'] ) : '';
return $instance;
}
}
// Register Popular Posts Widget
add_action( 'widgets_init', 'register_foo_widget' );
function register_foo_widget() {
register_widget( 'Foo_Widget' );
}
?>
Читай, получай, сравнивай
https://wp-kama.ru/function/get_the_date
https://wp-kama.ru/handbook/codex/date-time-api/date-formats
Получает дату создания текущего поста, не то.
Уже придумал как это сделать. Как сделаю закину сюда.