<?php
/*
Template name: Popular posts
*/ 
get_header();
?>
<section id="main" role="main">
    <div class="container">
        <div class="row">
            <div class="col-sm-9 right-content">
                <div class="row">
                    <?php if($redux_demo['ad1'] != '') {
                        echo '<div class="header_ad">' . $redux_demo['ad1'] . '</div>';
                    } ?>
                    <div class="col-sm-9">
                        <?php if($redux_demo['ad2'] != '') {
                            echo '<div class="header_ad">' . $redux_demo['ad2'] . '</div>';
                        } ?>
                        <div class="main-wrap">
                            <?php 
                            add_filter('posts_orderby','teo_ordervotes');
                            $args = array();
                            $args['post_type'] = 'post';
                            $args['meta_key'] = '_ratings_up';
                            $args['posts_per_page'] = 5;
                            $args['meta_query'] = array(
                                array('key' => '_ratings_up'),
                                array('key' => '_ratings_down')
                            );
                            //performance improvements
                            $args['update_post_term_cache'] = true;
                            $args['update_post_meta_cache'] = true;

                            $paged = is_front_page() ? get_query_var( 'page' ) : get_query_var( 'paged' );
                            $args['paged'] = $paged;
                            query_posts($args);
                            if ( have_posts() ) :
                                $count = 1;
                                while ( have_posts() ) : the_post();
                                    global $post; 
                                    if($count == 3 && $redux_demo['ad3'] != '') {
                                        echo '<div class="header_ad ad_between">' . $redux_demo['ad3'] . '</div>';
                                    }
                                    get_template_part( 'content', get_post_format() );
                                    $count++;
                                endwhile;
                                get_template_part('includes/pagination');
                            endif; wp_reset_query();
                            remove_filter('posts_orderby', 'teo_ordervotes');
                            ?>
                        </div>
                    </div>
                    <?php get_sidebar('right');?>
                </div>
            </div>
            <?php get_sidebar('left');?>
        </div>
    </div>
</section>
<?php get_footer();?>