Search code examples
wordpresswoothemes

Modify WooTabs Popular Posts to work with pageviews rather than comment counts


The woo tabs widget display the popular posts based on comment counts rather than page views. The code from the widget than enables this is

$popular = get_posts( array( 'suppress_filters' => false, 'ignore_sticky_posts' => 1, 'orderby' => 'comment_count', 'numberposts' => $posts ) ); foreach($popular as $post) : setup_postdata($post); ?>

Can anyone help me modify thsi so that it works with page views rather than comment counts???


Solution

  • I had the same issue and after a bit of research came up with the following:

    I added this snippet to my functions.php file:

    Track post views without a plugin using post meta

    Then from step 2 of that link I pasted this code snippet above the widget code:

    <?php setPostViews(get_the_ID()); ?>
    

    And finally changed 'orderby' => 'comment_count' to 'orderby' => 'post_views' as below:

    $popular = get_posts( array( 'suppress_filters' => false, 'ignore_sticky_posts' => 1, 'orderby' => 'post_views', 'numberposts' => $posts ) );
        foreach($popular as $post) : setup_postdata($post);
    

    Hope this helps :)