0

I have a WP site using Elementor Pro. I have created a Custom Post Type called "Meeting"

I have created an archive page for "Meetings" using a Loop Grid To display the post thumbnails, a title and a "read more" link.

At the side of this I want an Archive Widget in the side bar to show a list of years (that contain at least one "meeting"). I just want a list of years that when you click on a year it just shows posts for that year.

Does anyone know how I can achieve this?

1 Answer 1

1

First, create the custom WordPress widget. This will query all the years that have at least one post for your custom post type "Meeting." Save the following code in your theme's functions.php file:

class Meeting_Archive_Widget extends WP_Widget {

    public function __construct() {
        parent::__construct('meeting_archive', 'Meeting Archive', array('description' => 'List Meetings by Year'));
    }

    public function widget($args, $instance) {
        global $wpdb;
        $query = "SELECT YEAR(post_date) AS `year` FROM $wpdb->posts WHERE post_type='meeting' AND post_status='publish' GROUP BY `year` ORDER BY post_date DESC";
        $years = $wpdb->get_results($query);
        
        echo $args['before_widget'];
        echo $args['before_title'] . 'Meeting Archives' . $args['after_title'];

        echo '<ul>';
        foreach ($years as $year) {
            echo '<li><a href="' . get_post_type_archive_link('meeting') . '?year=' . $year->year . '">' . $year->year . '</a></li>';
        }
        echo '</ul>';

        echo $args['after_widget'];
    }
}

add_action('widgets_init', function(){
    register_widget('Meeting_Archive_Widget');
});

Next, modify the main Meetings query on your archive page to account for the year URL parameter. You could add this code also in your functions.php:

add_action('pre_get_posts', function($query){
    if (!is_admin() && $query->is_main_query() && is_post_type_archive('meeting')) {
        if (isset($_GET['year'])) {
            $query->set('year', $_GET['year']);
        }
    }
});

What this does is look for the year parameter in the URL and filters the meetings post type displayed based on that year.

And that's it! I haven't tested the code but in theory that should do the trick for you. If you encounter some issues just adjust the code accordingly.

1
  • Sorry but I didn't understand what you mean by create a custom Wordpress Widget. Do I do this in Elementor?
    – Jasperjon
    Sep 23, 2023 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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