Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Danijel Salijević
Danijel Salijević
8,120 Points

Most popular post

Hi, can someone help me to set up arguments to display most popular ( most readed )posts.

I use code arguments bellow but is not displaying anything. Mybe I need something to turn on that this code works propetly?

$args=array( 'post_type'=>'post', 'cat'=> -1 , 'meta_key'=>'post_views_count' , 'orderby' => 'meta_value_num' , 'posts_per_page' => 3 );

Thank you in advance

HI Danijel,

Have you added the WordPress loop using the WP_Query($args) object?

The $args array doesn't do anything by itself.

<?php 

        /**
         * The WordPress Query class.
         * @link http://codex.wordpress.org/Function_Reference/WP_Query
         *
         */
        $args = array(

            'post_type'=>'post',
            'cat'=> -1 ,
            'meta_key'=>'post_views_count' ,
            'orderby' => 'meta_value_num' ,
            'posts_per_page' => 3 

        );

    $wp_query = new WP_Query( $args );

    if ( have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    <!-- post -->
    <?php endwhile; ?>
    <!-- post navigation -->
    <?php else: ?>
    <!-- no posts found -->
    <?php endif; ?>


 ?>

1 Answer

Danijel Salijević
Danijel Salijević
8,120 Points

Yes, sure. But I think that problem is in arguments list . I know that this can be made with plugin, but this is not solution that I'm looking for.