Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

For a more accurate post view count, check out this postthis post I have done on the subject. It is very accurate and does not count double views

You can also use the approach described in this answer by @ialocinthis answer by @ialocin to one of my questions to apply and remove a filter using pre_get_posts. This code can be very easily adapted to be used on your custom query. I personally think that this is a better method to use to apply and to remove your custom filter for your custom query

For a more accurate post view count, check out this post I have done on the subject. It is very accurate and does not count double views

You can also use the approach described in this answer by @ialocin to one of my questions to apply and remove a filter using pre_get_posts. This code can be very easily adapted to be used on your custom query. I personally think that this is a better method to use to apply and to remove your custom filter for your custom query

For a more accurate post view count, check out this post I have done on the subject. It is very accurate and does not count double views

You can also use the approach described in this answer by @ialocin to one of my questions to apply and remove a filter using pre_get_posts. This code can be very easily adapted to be used on your custom query. I personally think that this is a better method to use to apply and to remove your custom filter for your custom query

Add edit 3
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

EDIT 3

You can also use the approach described in this answer by @ialocin to one of my questions to apply and remove a filter using pre_get_posts. This code can be very easily adapted to be used on your custom query. I personally think that this is a better method to use to apply and to remove your custom filter for your custom query

EDIT 3

You can also use the approach described in this answer by @ialocin to one of my questions to apply and remove a filter using pre_get_posts. This code can be very easily adapted to be used on your custom query. I personally think that this is a better method to use to apply and to remove your custom filter for your custom query

Add edit 2
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

EDIT 2

I have tested your code, and it does break my sidebar content. I have modified your code to the code below. It does work as expected, only posts from the last thirty are retrieved according to the fact that it has post thumbnails and ordered by highest post count. I am also testing this on localhost. One other thing that you need to take note of, you have to have Wordpress 3.9+ for this to work

You don't have to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up.

<?php if (is_home()) : ?>
    <section class="featured-posts clear">
        <?php
        function filter_where($where = '') {
        //posts in the last 30 days
            $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
            return $where;
        }

        add_filter('posts_where', 'filter_where');

        $args = array(
            'order'             => 'DESC',
            'posts_per_page'    => 20,
            'meta_key'          => 'post_views_count',
            'orderby'           => 'meta_value_num',
            'meta_query'        => array(
                'relation'      => 'AND',
                array(
                    'key' => 'post_views_count',
                    'compare' => 'EXISTS'
                ),
                array(
                    'key' => '_thumbnail_id',
                    'compare' => 'EXISTS'
                ),
            ),
        );
        $popularPosts = new WP_Query( $args );

        remove_filter('posts_where', 'filter_where');

        $counter = 1; ?>
        
        <?php while ($popularPosts->have_posts() ) : $popularPosts->the_post(); ?>
        <article class="box-<?php echo $counter++; ?>">
            <a href=" <?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?>
                <span><?php the_title(); ?></span>
            </a>
        </article>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
        
    </section>
<?php endif; // is_home ?>

EDIT 2

I have tested your code, and it does break my sidebar content. I have modified your code to the code below. It does work as expected, only posts from the last thirty are retrieved according to the fact that it has post thumbnails and ordered by highest post count. I am also testing this on localhost. One other thing that you need to take note of, you have to have Wordpress 3.9+ for this to work

You don't have to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up.

<?php if (is_home()) : ?>
    <section class="featured-posts clear">
        <?php
        function filter_where($where = '') {
        //posts in the last 30 days
            $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
            return $where;
        }

        add_filter('posts_where', 'filter_where');

        $args = array(
            'order'             => 'DESC',
            'posts_per_page'    => 20,
            'meta_key'          => 'post_views_count',
            'orderby'           => 'meta_value_num',
            'meta_query'        => array(
                'relation'      => 'AND',
                array(
                    'key' => 'post_views_count',
                    'compare' => 'EXISTS'
                ),
                array(
                    'key' => '_thumbnail_id',
                    'compare' => 'EXISTS'
                ),
            ),
        );
        $popularPosts = new WP_Query( $args );

        remove_filter('posts_where', 'filter_where');

        $counter = 1; ?>
        
        <?php while ($popularPosts->have_posts() ) : $popularPosts->the_post(); ?>
        <article class="box-<?php echo $counter++; ?>">
            <a href=" <?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?>
                <span><?php the_title(); ?></span>
            </a>
        </article>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
        
    </section>
<?php endif; // is_home ?>
Edit code
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209
Loading
Edit code
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209
Loading
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209
Loading