esc_html__( 'A widget that show most viewed posts', 'bastet-blocks' ), ) ); } /** * How to display the widget on the screen. */ function widget( $args, $instance ) { extract( $args ); global $wpdb, $period_posts; $time_id = rand(); /* Our variables from the widget settings. */ $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : esc_html__( 'Most Liked', 'bastet-blocks' ); $title = apply_filters ('widget_title', $instance ['title']); $num_posts = ( ! empty( $instance['num_posts'] ) ) ? absint( $instance['num_posts'] ) : 4; if ( ! $num_posts ) { $num_posts = 4; } $period_posts = ( ! empty( $instance['period_posts'] ) ) ? absint( $instance['period_posts'] ) : 0; if ( ! $period_posts ) { $period_posts = 0; } $categories = ( ! empty( $instance['categories'] ) ) ? absint( $instance['categories'] ) : ''; echo ''.$before_widget; /* Display the widget title if one was input (before and after defined by themes). */ if ( $title ){ echo ''.$before_title.$title.$after_title; } ?> 0 ) : // Create a new filtering function that will add our where clause to the query if ( !function_exists( 'bastet_widget_filter_where' ) ) { function bastet_widget_filter_where( $where = '' ) { global $period_posts; // posts in the last N days $bastet_widget_days = '-'.$period_posts.' days'; $where .= " AND post_date > '" . date('Y-m-d', strtotime($bastet_widget_days)) . "'"; return $where; } add_filter('posts_orderby', 'bastet_widget_filter_orderby'); add_filter( 'posts_where', 'bastet_widget_filter_where' ); } endif; $popular_posts = new WP_Query(array( 'showposts' => $num_posts, 'cat' => $categories, 'orderby' => 'meta_value_num', 'meta_key' => 'bs_views', 'ignore_sticky_posts' => 1 )); if ( $period_posts > 0 ) : remove_filter('posts_orderby', 'bastet_widget_filter_orderby'); remove_filter( 'posts_where', 'bastet_widget_filter_where' ); endif; ?> have_posts() ) : ?> esc_html__( 'Most Popular' , 'bastet-blocks' ), 'num_posts' => 4, 'period_posts' => 0, 'categories' => '', ); $instance = wp_parse_args((array) $instance, $defaults); $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; ?>