Created
May 11, 2021 10:18
-
-
Save wbcomdev/7c6eb8d5021de13d5e2bcd7b9e80eeed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Wb WordPress Widget for display Posts with different filter. | |
| */ | |
| class Wb_Display_Post_Diff_Fillter extends WP_Widget { | |
| /** | |
| * Working as display post, we get things done better. | |
| * | |
| * @since 1.0.3 | |
| */ | |
| public function __construct() { | |
| parent::__construct( | |
| 'Wb_Display_Post_Diff_Fillter', | |
| __( 'Display Post', 'wbcom' ), | |
| array( 'description' => __( 'Sample widget based on recent modify post', 'wbcom' ) ) | |
| ); | |
| } | |
| /** | |
| * Extends our front-end output method. | |
| * | |
| * @since 1.0.3 | |
| * | |
| * @param array $args Array of arguments for the widget. | |
| * @param array $instance Widget instance data. | |
| */ | |
| public function widget( $args, $instance ) { | |
| $order = esc_attr( $instance['order'] ); | |
| ?> | |
| <?php | |
| // The Loop. | |
| if ( 'title' === $order ) { | |
| $wq = new WP_Query(); | |
| $wq->query( | |
| array( | |
| 'post_type' => 'post', | |
| 'posts_per_page' => 5, | |
| 'orderby' => $order, | |
| 'order' => 'ASC', | |
| ) | |
| ); | |
| if ( $wq->have_posts() ) : | |
| ?> | |
| <section class="widget widget_archive"> | |
| <ul> | |
| <?php | |
| while ( $wq->have_posts() ) : | |
| $wq->the_post(); | |
| ?> | |
| <li> | |
| <h3><a href="<?php the_permalink(); ?>" rel="bookmark"><?php echo the_post_thumbnail( 'medium' ); ?></a></h3> | |
| <h3><a href="<?php the_permalink(); ?>" rel="bookmark"><?php echo get_the_title(); ?></a></h3> | |
| <h3><a href="<?php the_permalink(); ?>" rel="bookmark"><?php echo get_the_content(); ?></a></h3> | |
| </li> | |
| <?php endwhile; ?> | |
| </ul> | |
| </section> | |
| <?php | |
| endif; | |
| wp_reset_postdata(); | |
| } elseif ( 'comment_count' === $order ) { | |
| global $wpdb; | |
| $result = $wpdb->get_results( | |
| 'select wp_posts.*, max(comment_date) as comment_date | |
| from wp_posts wp_posts right join wp_comments on id = comment_post_id where wp_posts.comment_count >0 group by ID order by comment_date desc' | |
| ); | |
| ?> | |
| <section class="widget widget_archive"> | |
| <?php | |
| foreach ( $result as $value ) { | |
| ?> | |
| <h3><a href="<?php echo $value->guid; ?>" rel="bookmark"> <?php echo $value->post_title; ?></a></h3> | |
| <h3><a href="<?php echo $value->guid; ?>" rel="bookmark"> <?php echo $value->post_content; ?></a></h3> | |
| <?php | |
| } | |
| ?> | |
| </section> | |
| <?php | |
| } else { | |
| $wq = new WP_Query(); | |
| $wq->query( | |
| array( | |
| 'post_type' => 'post', | |
| 'posts_per_page' => 5, | |
| 'orderby' => $order, | |
| ) | |
| ); | |
| if ( $wq->have_posts() ) : | |
| ?> | |
| <section class="widget widget_archive"> | |
| <ul> | |
| <?php | |
| while ( $wq->have_posts() ) : | |
| $wq->the_post(); | |
| ?> | |
| <li> | |
| <h3><a href="<?php the_permalink(); ?>" rel="bookmark"><?php echo the_post_thumbnail( 'medium' ); ?></a></h3> | |
| <h3><a href="<?php the_permalink(); ?>" rel="bookmark"><?php echo get_the_title(); ?></a></h3> | |
| <h3><a href="<?php the_permalink(); ?>" rel="bookmark"><?php echo get_the_content(); ?></a></h3> | |
| </li> | |
| <?php endwhile; ?> | |
| </ul> | |
| </section> | |
| <?php | |
| endif; | |
| wp_reset_postdata(); | |
| } | |
| } | |
| /** | |
| * Extends our update method. | |
| * | |
| * @since 1.0.3 | |
| * | |
| * @param array $new_instance New instance data. | |
| * @param array $old_instance Original instance data. | |
| * @return array | |
| */ | |
| public function update( $new_instance, $old_instance ) { | |
| return $new_instance; | |
| } | |
| /** | |
| * Extends our form method. | |
| * | |
| * @since 1.0.3 | |
| * | |
| * @param array $instance Current instance. | |
| * @return mixed | |
| */ | |
| public function form( $instance ) { | |
| $order = apply_filters( 'widget_order', $instance['order'] ); | |
| ?> | |
| <p> | |
| <label for="<?php echo $this->get_field_id( 'order' ); ?>"><?php echo esc_html_e( 'Choose Display Post:', 'wbcom' ); ?></label> | |
| <select name="<?php echo $this->get_field_name( 'order' ); ?>"> | |
| <option value="title" | |
| <?php | |
| if ( 'title' === $order ) { | |
| echo 'selected="selected"'; } | |
| ?> | |
| ><?php echo esc_html_e( 'Alphabetic Posts', 'wbcom' ); ?> </option> | |
| <option value="modified" | |
| <?php | |
| if ( 'modified' === $order ) { | |
| echo 'selected="selected"'; } | |
| ?> | |
| ><?php echo esc_html_e( 'Modified Posts', 'wbcom' ); ?></option> | |
| <option value="comment_count" | |
| <?php | |
| if ( 'comment_count' === $order ) { | |
| echo 'selected="selected"'; } | |
| ?> | |
| ><?php echo esc_html_e( 'Recent Commented Posts', 'wbcom' ); ?></option> | |
| </select> | |
| </p> | |
| <?php | |
| } | |
| } | |
| /** | |
| * Register the widget. | |
| */ | |
| function wb_register_post_filter_widget() { | |
| register_widget( 'Wb_Display_Post_Diff_Fillter' ); | |
| } | |
| add_action( 'widgets_init', 'wb_register_post_filter_widget' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment