0

for my club's website I have a list of upcoming events. Every event is set with the date it will take place, so I'll have ordered it in archive.

I already prepared the site fo view future posts via functions.php. It correctly shows all my events in chronological order if call them in archive.php

In my homepage I'd like to show the next 3 events coming: a list of the first future event followed by the next 2 near events.

EXAMPLE in 2018 I have one event on february, one on april, one on may, one on june ond one on october.

  • if i visite the site today (march) I want the homepage to show the events on april, may and june

  • visiting the site in april I want the site to show the events on may, june and october

How can I obtain it?

THE CODE

if ( ! class_exists( 'Business_Club_Latest_News_Widget' ) ) :

/**
 * Latest news widget class.
 *
 * @since 1.0.0
 */
class Business_Club_Latest_News_Widget extends WP_Widget {

    /**
     * Constructor.
     *
     * @since 1.0.0
     */
    function __construct() {
        $opts = array(
            'classname'                   => 'business_club_widget_latest_news',
            'description'                 => esc_html__( 'Latest News Widget. Displays latest posts in grid.', 'business-club' ),
            'customize_selective_refresh' => true,
            );

        parent::__construct( 'business-club-latest-news', esc_html__( 'BC: Latest News', 'business-club' ), $opts );
    }

    /**
     * Echo the widget content.
     *
     * @since 1.0.0
     *
     * @param array $args     Display arguments including before_title, after_title,
     *                        before_widget, and after_widget.
     * @param array $instance The settings for the particular instance of the widget.
     */
    function widget( $args, $instance ) {

        $title          = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
        $subtitle       = ! empty( $instance['subtitle'] ) ? $instance['subtitle'] : '';
        $post_category  = ! empty( $instance['post_category'] ) ? $instance['post_category'] : 0;
        $post_column    = ! empty( $instance['post_column'] ) ? $instance['post_column'] : 4;
        $featured_image = ! empty( $instance['featured_image'] ) ? $instance['featured_image'] : 'business-club-thumb';
        $post_number    = ! empty( $instance['post_number'] ) ? $instance['post_number'] : 4;
        $excerpt_length = ! empty( $instance['excerpt_length'] ) ? $instance['excerpt_length'] : 0;
        $more_text      = ! empty( $instance['more_text'] ) ? $instance['more_text'] : '';

        echo $args['before_widget'];

        // Display widget title.
        if ( $title ) {
            echo $args['before_title'] . $title . $args['after_title'];
        }

        // Display widget subtitle.
        if ( $subtitle ) {
            echo '<h3 class="subtitle">' . esc_html( $subtitle ) . '</h3>';
        }

        $qargs = array(
            'posts_per_page'      => esc_attr( $post_number ),
            'no_found_rows'       => true,
            'ignore_sticky_posts' => true,
        );

        if ( absint( $post_category ) > 0 ) {
            $qargs['cat'] = absint( $post_category );
        }

        $the_query = new WP_Query( $qargs );
        ?>
        <?php if ( $the_query->have_posts() ) : ?>

            <div class="latest-news-widget latest-news-col-<?php echo esc_attr( $post_column ); ?>">

                <div class="inner-wrapper">

                    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                        <div class="latest-news-item">
                            <div class="latest-news-wrapper">
                                <?php if ( 'disable' !== $featured_image && has_post_thumbnail() ) : ?>
                                    <div class="latest-news-thumb">
                                        <a href="<?php the_permalink(); ?>">
                                            <?php
                                            $img_attributes = array( 'class' => 'aligncenter' );
                                            the_post_thumbnail( esc_attr( $featured_image ), $img_attributes );
                                            ?>
                                        </a>
                                    </div><!-- .latest-news-thumb -->
                                    <div class="latest-news-meta">
                                        <span class="posted-on"><?php the_time( get_option( 'date_format' ) ); ?></span>
                                        <?php
                                        if ( comments_open( get_the_ID() ) ) {
                                            echo '<span class="comments-link">';
                                            comments_popup_link( '<span class="leave-reply">' . esc_html__( '0 Comment', 'business-club' ) . '</span>', esc_html__( '1 Comment', 'business-club' ), esc_html__( '% Comments', 'business-club' ) );
                                            echo '</span>';
                                        }
                                        ?>
                                    </div><!-- .latest-news-meta -->

                                <?php endif; ?>
                                <div class="latest-news-text-wrap">
                                    <h3 class="latest-news-title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h3>

                                    <?php if ( absint( $excerpt_length ) > 0 ) : ?>
                                        <div class="latest-news-summary">
                                            <?php
                                            $excerpt = business_club_get_the_excerpt( absint( $excerpt_length ) );
                                            echo wp_kses_post( wpautop( $excerpt ) );
                                            ?>
                                        </div><!-- .latest-news-summary -->
                                    <?php endif; ?>
                                    <?php if ( ! empty( $more_text ) ) : ?>
                                        <div class="latest-news-read-more">
                                            <a href="<?php the_permalink(); ?>" class="read-more"><?php echo esc_html( $more_text ); ?></a>
                                        </div>
                                    <?php endif; ?>
                                </div><!-- .latest-news-text-wrap -->
                            </div><!-- .latest-news-wrapper -->
                        </div><!-- .latest-news-item -->

                    <?php endwhile; ?>

                </div><!-- .inner-wrapper -->
            </div><!-- .latest-news-widget -->

            <?php wp_reset_postdata(); ?>

        <?php endif; ?>
        <?php
        echo $args['after_widget'];

    }

    /**
     * Update widget instance.
     *
     * @since 1.0.0
     *
     * @param array $new_instance New settings for this instance as input by the user.
     * @param array $old_instance Old settings for this instance.
     * @return array Settings to save or bool false to cancel saving.
     */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        $instance['title']          = sanitize_text_field( $new_instance['title'] );
        $instance['subtitle']       = sanitize_text_field( $new_instance['subtitle'] );
        $instance['post_category']  = absint( $new_instance['post_category'] );
        $instance['post_number']    = absint( $new_instance['post_number'] );
        $instance['post_column']    = absint( $new_instance['post_column'] );
        $instance['excerpt_length'] = absint( $new_instance['excerpt_length'] );
        $instance['featured_image'] = sanitize_text_field( $new_instance['featured_image'] );
        $instance['more_text']      = sanitize_text_field( $new_instance['more_text'] );

        return $instance;
    }

    /**
     * Output the settings update form.
     *
     * @since 1.0.0
     *
     * @param array $instance Current settings.
     */
    function form( $instance ) {

        // Defaults.
        $instance = wp_parse_args( (array) $instance, array(
            'title'          => '',
            'subtitle'       => '',
            'post_category'  => '',
            'post_column'    => 4,
            'featured_image' => 'business-club-thumb',
            'post_number'    => 4,
            'excerpt_length' => 40,
            'more_text'      => esc_html__( 'Read more', 'business-club' ),
        ) );
        ?>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'business-club' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'subtitle' ) ); ?>"><?php esc_html_e( 'Subtitle:', 'business-club' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subtitle' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'subtitle' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['subtitle'] ); ?>" />
        </p>
        <p>
            <label for="<?php echo  esc_attr( $this->get_field_id( 'post_category' ) ); ?>"><?php esc_html_e( 'Select Category:', 'business-club' ); ?></label>
            <?php
            $cat_args = array(
                'orderby'         => 'name',
                'hide_empty'      => true,
                'taxonomy'        => 'category',
                'name'            => $this->get_field_name( 'post_category' ),
                'id'              => $this->get_field_id( 'post_category' ),
                'selected'        => $instance['post_category'],
                'show_option_all' => esc_html__( 'All Categories','business-club' ),
            );
            wp_dropdown_categories( $cat_args );
            ?>
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'post_number' ) ); ?>"><?php esc_html_e( 'Number of Posts:', 'business-club' ); ?></label>
            <input id="<?php echo esc_attr( $this->get_field_id( 'post_number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'post_number' ) ); ?>" type="number" value="<?php echo esc_attr( $instance['post_number'] ); ?>" min="1" max="20" />
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'post_column' ) ); ?>"><?php esc_html_e( 'Number of Columns:', 'business-club' ); ?></label>
            <?php
            $dropdown_args = array(
                'id'       => $this->get_field_id( 'post_column' ),
                'name'     => $this->get_field_name( 'post_column' ),
                'selected' => $instance['post_column'],
            );
            business_club_render_select_dropdown( $dropdown_args, 'business_club_get_numbers_dropdown_options', array( 'min' => 3, 'max' => 4 ) );
            ?>
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'featured_image' ) ); ?>"><?php esc_html_e( 'Select Image Size:', 'business-club' ); ?></label>
            <?php
            $dropdown_args = array(
                'id'       => $this->get_field_id( 'featured_image' ),
                'name'     => $this->get_field_name( 'featured_image' ),
                'selected' => $instance['featured_image'],
            );
            business_club_render_select_dropdown( $dropdown_args, 'business_club_get_image_sizes_options', array( 'add_disable' => false ) );
            ?>
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'excerpt_length' ) ); ?>"><?php esc_html_e( 'Excerpt Length:', 'business-club' ); ?></label>
            <input id="<?php echo esc_attr( $this->get_field_id( 'excerpt_length' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'excerpt_length' ) ); ?>" type="number" value="<?php echo esc_attr( $instance['excerpt_length'] ); ?>" min="0" max="200" />&nbsp;<small><?php esc_html_e( 'in words', 'business-club' ); ?></small>
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'more_text' ) ); ?>"><?php esc_html_e( 'Read More Text:', 'business-club' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'more_text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'more_text' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['more_text'] ); ?>" />
        </p>
        <?php
    }

}

endif;

0

You should be able to get the next 3 posts by requesting the future status.

$args = array(
    'post_type'       => 'post',
    'post_status'     => 'future',
    'posts_per_page'  => 3,
    'orderby'         => 'date',
    'order'           => 'ASC',
);
$future_posts = new WP_Query( $args );

References

  • ps: I added the code I want to modify in the first post. – resunoiz Mar 22 '18 at 21:15
  • not working. it show future events but PAST events too. I want to show only events that are in the future, but it show pasts events too. – resunoiz Mar 22 '18 at 21:33
  • I'm not sure what to tell you. I just tried my code on a stock WordPress installation, and it only returned my future posts. – Pat J Mar 26 '18 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.