<div class="custom_sidebar">
    <!-- Best Articles Section -->
    <div class="target_img">
        <img src="<?php echo THEME_IMG_DIR ?>banner.png" alt="">
    </div>
    <div class="row">
        <div class="col-lg-12 col-md-6 col-sm-12">
            <div class="in_article">
                <h3><?php _e('أفضل المقالات', 'saint') ?></h3>

                <ul>
                    <?php
                    global $wpdb;
        $results = $wpdb->get_results("
        SELECT ID, post_title, post_views_count 
        FROM {$wpdb->prefix}posts 
        WHERE post_type = 'ourblog' 
        AND post_status = 'publish' 
        ORDER BY post_views_count DESC 
        LIMIT 3
    ");

        $i = 0.2;

        foreach ($results as $post) :
            setup_postdata($post);
            ?>
                        <li class="wow bounceInRight" data-wow-delay="<?php echo $i; ?>s">
                            <a href="<?php echo get_permalink($post->ID); ?>">
                                <?php if (has_post_thumbnail($post->ID)) : ?>
                                    <div class="post-thumbnail">
                                        <?php echo get_the_post_thumbnail($post->ID, 'thumbnail'); ?>
                                    </div>
                                <?php endif; ?>
                                <div class="post-title"><?php echo esc_html($post->post_title); ?></div>
                            </a>
                        </li>
                        <script>
                        </script>
                        <?php
            $i += 0.2;
        endforeach;

        // Reset post data
        wp_reset_postdata();
        ?>
                </ul>




            </div>
        </div>

        <!-- Latest Articles Section -->
        <div class="col-lg-12 col-md-6 col-sm-12">
            <div class="in_article">
                <h3><?php _e('أحدث المقالات', 'saint') ?></h3>
                <ul>
                    <?php
        // Fetch the latest articles using get_posts()
        $latest_articles = get_posts(array(
            'post_type' => 'ourblog',
            'posts_per_page' => 3,
            'orderby' => 'date',
            'order' => 'DESC'
        ));

        // Initialize the delay time
        $i = 0.2;

        // Loop through each post
        foreach ($latest_articles as $post) :
            setup_postdata($post);
            ?>
                        <li class="wow <?php the_switch_language('bounceInRight', 'bounceInLeft'); ?>"
                            data-wow-delay="<?php echo $i; ?>s">
                            <a href="<?php the_permalink(); ?>">
                                <?php if (has_post_thumbnail()) : ?>
                                    <div class="post-thumbnail">
                                        <?php the_post_thumbnail('thumbnail'); ?>
                                    </div>
                                <?php endif; ?>
                                <div class="post-title"><?php the_title(); ?></div>
                            </a>
                        </li>
                        <?php
            // Increment the delay time
            $i += 0.2;
        endforeach;

        // Reset the global post data
        wp_reset_postdata();
        ?>
                </ul>
            </div>
        </div>
    </div>
</div>
