<?php
/**
 * Sidebar
 *
 * @link https://krzysztof-turek.com
 *
 * @package tylkofotografia.pl
 * @version 0.2
 */
global $post;
global $wpdb;
$postLessViewed = 0;
$postRandViewed = 0;

$upload_dir = wp_upload_dir();
$currentPost_id = get_the_ID();

$most = $wpdb->get_row(
    "SELECT sum(count) views, id FROM `wp_statistics_pages`  where id not in ( 0,1, $currentPost_id) and type = 'post' group BY `wp_statistics_pages`.`id` order by views desc limit 1"
);

$postMostViewed = get_post( $most->id ); 
$less = $wpdb->get_row(
    "SELECT sum(count) views, id FROM `wp_statistics_pages`  where id not in ( 0,1, $currentPost_id) and type = 'post' group BY `wp_statistics_pages`.`id` order by views asc limit 1"
);
if ($less) {
    $postLessViewed = get_post( $less->id );
} 

$rand = $wpdb->get_row(
    "SELECT * FROM `wp_posts` where post_status = 'publish' and post_type = 'post' and id not in ( $postMostViewed->ID, $postLessViewed->ID, $currentPost_id ) order by rand() limit 1"
);
if ($rand) {
    $postRandViewed = get_post( $rand->ID ); 
} 
?>

<div style="margin-bottom:40px">
   <form class="d-flex input-group w-auto my-auto mb-3 mb-md-0"  method="get" action="/index.php">
      <input autocomplete="off" type="search" name="s" class="form-control rounded" placeholder="Szukaj..." />
      <button class="input-group-text border-0 d-none d-lg-flex" type="submit"><i class="fas fa-search"></i></button>
   </form>
</div>
<div style="margin-bottom:40px">
   <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2401815550643539"
      crossorigin="anonymous"></script>
   <!-- Sidebar -->
   <ins class="adsbygoogle"
      style="display:block"
      data-ad-client="ca-pub-2401815550643539"
      data-ad-slot="6894360927"
      data-ad-format="auto"
      data-full-width-responsive="true"></ins>
   <script>
      (adsbygoogle = window.adsbygoogle || []).push({});
   </script>
</div>
<div>
	<h4>
		Zobacz również
	</h4>
	<a href="<?php echo esc_url( get_permalink($postMostViewed->ID) ); ?>">
	<?php echo get_the_post_thumbnail( $postMostViewed->ID, 'thumbnail', array( 'class' => 'alignleft' ) ); ?>
	</a>
	<p>

	</p>
	<a href="<?php echo esc_url( get_permalink($postLessViewed->ID) ); ?>">
	<?php echo get_the_post_thumbnail( $postLessViewed->ID, 'thumbnail', array( 'class' => 'alignleft' ) ); ?>
	</a>
	<p>

	</p>
	<a href="<?php echo esc_url( get_permalink($postRandViewed->ID) ); ?>">
	<?php echo get_the_post_thumbnail( $postRandViewed->ID, 'thumbnail', array( 'class' => 'alignleft' ) ); ?>
	</a>

	
</div>
</div>
