WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Simple increment in post views. (7 posts)

  1. southcast
    Member
    Posted 1 year ago #

    I have a following code on my single.php which gives me the total count of the post views. It works just fine.

    <?php echo 'TOTAL POST VIEWS: ' .wpfp_get_current_count(); ?>

    Now, I would like to add 300 views by default. So that if my actual views are 33 then the total post views becomes 333. I guess i need a lesson on some basic php addition. Could somebody please help a little here please. If possible please suggest me a modified code.
    Always thankful to this community.

  2. catacaustic
    Member
    Posted 1 year ago #

    Well, some people don't have the chance to respond quite so quickly sometimes, but we try when we can. :)

    Now, your problem is pretty easy really. All you need is something like this:

    <?php echo 'TOTAL POST VIEWS: ' .(wpfp_get_current_count() + 300); ?>

    You may not need the () around the addition part, but I would always use it because it makes any future debugging a whole lot easier.

  3. southcast
    Member
    Posted 1 year ago #

    @Catacaustic. Thanks for trying but the code did not work. In fact I had tried this earlier. wpfp_get_current_count() still works and displays the actual post views but the addition does not happen. Any suggestion ?

  4. catacaustic
    Member
    Posted 1 year ago #

    <?php
    function wpfp_get_current_count () {
        return 40;
    }
    echo 'TOTAL POST VIEWS '.(wpfp_get_current_count () + 300);

    Using that as an example, it displays the count as 340, so I know that code works (I did actually test this one).

    The only reason that I can think of that might do anything differently is if the wpfp_get_current_count() function echos the total and doesn't return it as it normally should. I'd suggest that you dig around in that function to see what it's actually doing.

  5. southcast
    Member
    Posted 1 year ago #

    Whoops ! This one throws a Fatal error: Cannot redeclare wpfp_get_current_count(). I will paste here what I have in my custom-functions.php. Kindly take a look and suggest me an appropriate modification in the code.

    function wpfp_get_current_count() {
        global $wpdb;
    	$current_post = get_the_ID();
        $query = "SELECT post_id, meta_value, post_status FROM $wpdb->postmeta";
        $query .= " LEFT JOIN $wpdb->posts ON post_id=$wpdb->posts.ID";
        $query .= " WHERE post_status='publish' AND meta_key='wpfp_favorites' AND post_id = '".$current_post."'";
        $results = $wpdb->get_results($query);
        if ($results) {
            foreach ($results as $o):
                echo $o->meta_value;
            endforeach;
        }else {echo( '0' );}
    }

    I really appreciate your help.

  6. catacaustic
    Member
    Posted 1 year ago #

    You see where your code says echo? That's the problem. You need to return that numeric value instead of printing it out. You need to return the value like I did in my example above.

  7. southcast
    Member
    Posted 1 year ago #

    Yep, that was the culprit. I recieved the same solution here just a few seconds ago. But I appreciate the effort you took to help. I am marking this resolved. Again, many thanks @catacaustic.

Topic Closed

This topic has been closed to new replies.

About this Topic