user2093301
user2093301

Reputation: 367

Wordpress wp_query instead of

I have this function that displays the 5 most viewed posts.

in functions.php i have:

// function to display number of posts.
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}

// function to count views.
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

At the moment i'm using a query_posts method to display the 5 most viewed posts:

<?php
query_posts( array (
   'post_type' => 'post', 
   'showposts' => 5,
   'meta_key' => 'post_views_count',
   'orderby' => 'meta_value_num',
   )
   );

   if (have_posts()) : while (have_posts()) : the_post();?>
       <a href="<?php the_permalink();?>"><?php the_title();?></a>      
   <?php endwhile; endif; wp_reset_query(); ?> 

And now I'm trying to achieve the same results by using wp_query, but it doesn't seem to be working.

This is the code i'm using for wp_query:

<?php $custom_query = new WP_Query('showposts=5, meta_key=post_views_count, orderby=meta_value_num'); // exclude category 9
while($custom_query->have_posts()) : $custom_query->the_post(); ?>

<a href="<?php the_permalink();?>"><?php the_title();?></a>     


<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>

It just displays the 5 latest posts and not the 5 most viewed posts. Can someone help me on this?

Upvotes: 1

Views: 906

Answers (2)

Andrew Bartel
Andrew Bartel

Reputation: 579

showsposts is no longer a valid parameter for WP_Query, from the documentation

posts_per_page (int) - number of post to show per page (available with Version 2.1, replaced showposts parameter).

As lorem monkey suggested, it's better to pass your parameters as an array as well.

<?php 

$args = array('posts_per_page'=>5,'meta_key'=>'post_views_count','orderby'=>'meta_value_num');
$custom_query = new WP_Query($args);
while($custom_query->have_posts()) : $custom_query->the_post(); ?>

<a href="<?php the_permalink();?>"><?php the_title();?></a>     


<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query 

?>

Upvotes: 1

lorem monkey
lorem monkey

Reputation: 3992

Try it with your arguments as an array:

$wpq_args = array(
    'post_type' => 'post',
    'showposts' => 5,
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num'
);
$custom_query = new WP_Query($wpq_args);

See the documentation for a few examples and all arguments: http://codex.wordpress.org/Class_Reference/WP_Query

Upvotes: 1

Related Questions