WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this code in my header.php and it is working:

<div id="news">
        <?php query_posts('category_name=news'); ?>
        <?php while (have_posts()) : the_post(); ?>
            <div class="new">
                <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('thumbnail'); ?>
                <a href="<?php the_permalink(); ?>">
                    <h3><?php the_title(); ?></h3>
                <?php the_excerpt(); ?>
                    <span class="autor">Author: <?php the_author(); ?></span>&nbsp;|&nbsp;
                    <span class="autor">Date: <?php the_date(); ?></span>&nbsp;|&nbsp;



        <?php endwhile; ?>

Now, I would like to do something like:

<span class="views">Views: <?php the_views(); ?></span>&nbsp;|&nbsp;

to show each post how many times was read. However, it is throwing an error. How to solve this?

share|improve this question
what is the exact error message? – Michael Oct 2 '12 at 10:12
Fatal error: Call to undefined function the_views() – Derfder Oct 2 '12 at 10:22
Is this not a build in function of WP? – Derfder Oct 2 '12 at 10:34
Also, don't use query_posts()! – Stephen Harris Oct 3 '12 at 9:25
up vote 1 down vote accepted

the_views() is not a built in function of WordPress. In fact, WordPress does not, by default, log the number of views of posts.

There are probably several plug-ins that exist which add this functionality. The only one I have ever used (and its one I've never had any problems with) is WP-Postviews http://wordpress.org/extend/plugins/wp-postviews/.

share|improve this answer

in function.php put this code

function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, '0');
    return "0 View";
return $count.' Views';}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
    $count = 0;
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, '0');
    update_post_meta($postID, $count_key, $count);

then in single.php put this code <?php setPostViews(get_the_ID()); ?>

to show views count put this code anywhere you want <span class="views"><?php echo getPostViews(get_the_ID()); ?></span>&nbsp;|&nbsp; that will count each visit to the post you can put it in the loop.php/index.php to view each post views

share|improve this answer
Hi @Shady - this - at the core of it, is essentially what wp-postviews does. But you might always want to think about post revisions, the same user viewing the same post, whether to count a view by an admin, or by a bot and how to deal with Caching plug-ins etc. – Stephen Harris Oct 3 '12 at 9:36
@StephenHarris thanks for your advise, i know its not perfect solution for the viewing count, i just wanted to share the code if any one like me dosn't like dozens of plugins running on his WordPress site so its simple . – Shady M Rasmy Oct 3 '12 at 15:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.