esc_html__( 'Displays popular posts based on the comments count. Use this widget in main sidebars.', 'bam' ), ) // Args ); } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { $defaults = array( 'title' => esc_html__( 'Popular Posts', 'bam' ), 'category' => 'all', 'number_posts' => 5, 'date_range' => '' ); $instance = wp_parse_args( (array) $instance, $defaults ); ?>
$this->get_field_name('category'), 'selected' => $instance['category'], 'show_option_all' => 'Show popular posts from all the categories' ) ); ?>
id_base ); $category = ( ! empty( $instance['category'] ) ) ? absint( $instance['category'] ) : 0; $number_posts = ( ! empty( $instance['number_posts'] ) ) ? absint( $instance['number_posts'] ) : 5; $date_range = ( ! empty( $instance['date_range'] ) ) ? absint( $instance['date_range'] ) : ''; $post_args = array( 'cat' => $category, 'ignore_sticky_posts' => 1, 'posts_per_page' => $number_posts, 'post_status' => 'publish', 'no_found_rows' => true, 'orderby' => 'comment_count', 'order' => 'desc' ); if( isset( $date_range ) && ! empty( $date_range ) ) { $post_args[ 'date_query' ] = array( array( 'after' => $date_range . ' days ago' ) ); } // Latest Posts $latest_posts = new WP_Query( $post_args ); echo $before_widget; ?>