Web Design Forum: [WP] Most popular post of the last 24 hours - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[WP] Most popular post of the last 24 hours Rate Topic: -----

#1 User is online   MikeChipshop 

  • Small but imperfectly formed
  • Group: Moderators
  • Posts: 7,045
  • Joined: 19-April 10
  • Reputation: 503
  • Gender:Male
  • Location:Scotland

Posted 30 January 2012 - 10:13 AM

Happy Monday morning chaps,

I've been working on a clients WP site which is an updated version of a custom theme i done for them early last year.

In the header of the main page they want to display several different section which were 'Editors Pick', 'Latest post' and 'Most popular post'.
I had these all done and dusted until they've come back to me and asked for a change to the popular post section.

At first we were counting comments to display the most popular post but they decided it would be better to use views to work out the most popular.
After a bit of hunting i found this...

Functions.php
/***************************************************
/ POST VIEWS
/***************************************************/

	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 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);
    }
}



Header.php
<?php query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC&showposts=1');?>

... call rest of loop as normal ...


This works brilliantly and displays the content of the most popular post by views.
Problem is now that the client has come back to me and requested that the post displayed should be the most popular post of the past 24hours... and that's where i'm stuck.

Anyone have any idea how to modify what i've got? Is there anyway to hook into WP cron to flush the post views count at a certain time every 24 hours? Arghghgh any help will be most appreciated.

Cheers guys.

Mike
0

#2 User is online   Spitfire 

  • Mighty Pirate™
  • PipPipPipPip
  • Group: Members
  • Posts: 891
  • Joined: 05-February 11
  • Reputation: 189
  • Gender:Male
  • Location:Berkshire
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 30 January 2012 - 11:14 AM

Just so you know, the Wordpress cron isn't a real cron. It's a pseudo-cron that requires site visits to work. Basically it schedules tasks and then when someone visits the site, checks the current time against the list and then runs any tasks that are overdue. So you won't get the exact functionality of a proper cron running at a specific time, but provided the site in question gets a bit of traffic (even from bots) it should be okay.

Anyway, enough waffle. Have a nose at wp_schedule_event(). You should just be able to set both those above functions to run daily.
0

#3 User is online   MikeChipshop 

  • Small but imperfectly formed
  • Group: Moderators
  • Posts: 7,045
  • Joined: 19-April 10
  • Reputation: 503
  • Gender:Male
  • Location:Scotland

Posted 30 January 2012 - 11:21 AM

View PostSpitfire, on 30 January 2012 - 11:14 AM, said:

<snip>


Cheers mate,
Yeah i'm aware of the way the WP cron works just not actually delved in to it at any point. Thankfully this site gets thousands of hits a day so it shouldn't be an issue (fingers crossed).

I'll dig through that codex entry (which i couldn't find at all!). Still not sure where i'm going to go from here though. If anyone has an idea of how i'm going to flush the count then i'll be grateful to hear all and any ideas. :p

Cheers again mate.

This post has been edited by MikeChipshop: 30 January 2012 - 11:23 AM

0

#4 User is online   brightonmike 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,996
  • Joined: 27-June 11
  • Reputation: 340
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 01 February 2012 - 10:11 AM

Ooo, this looks good for my music site. Might have to do some casual code theft.
0

#5 User is online   MikeChipshop 

  • Small but imperfectly formed
  • Group: Moderators
  • Posts: 7,045
  • Joined: 19-April 10
  • Reputation: 503
  • Gender:Male
  • Location:Scotland

Posted 01 February 2012 - 10:26 AM

Haha all good.

I'm still no further on how to make it only count the last 24 hours though :(
0

#6 User is online   brightonmike 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,996
  • Joined: 27-June 11
  • Reputation: 340
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 01 February 2012 - 10:47 AM

Sorry Mike, I really don't want to high-jack your thread (I'm gonna try and look around/Google to find a solution for you too) but I can't get your code to work in the first place:

			<li class="widget-container">
			
				<?php query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC&showposts=5'); ?>
 				<h3 class="side-widget-title">Most Popular Posts</h3>
				<ul>
				<?php while (have_posts()) : the_post(); ?>
				<li><h2 <?php post_class('posttitle'); ?>><a class="entry-title" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
				</li>	
				<?php endwhile; ?>
				</ul>
			</li>



Blueurtghgbhr!

Wait, before I post, I found this? http://wordpress.org...-popular-posts/
0

#7 User is online   MikeChipshop 

  • Small but imperfectly formed
  • Group: Moderators
  • Posts: 7,045
  • Joined: 19-April 10
  • Reputation: 503
  • Gender:Male
  • Location:Scotland

Posted 01 February 2012 - 10:54 AM

No problem man, feel free to bump my thread all you want ;)

The exact code i am using is in the header.php file.
The function is as in the first post and the loop i call is as soooo...

<?php query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC&showposts=1');?>
<?php while (have_posts()) : the_post() ;?>
	<div id="popularpost">
    	<div id="popularpost-image">
    		<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
			<?php 
				if ( has_post_thumbnail() ) the_post_thumbnail('header-small-thumb');
				else {
			?>
            <img src="<?php bloginfo( 'template_directory' ); ?>/images/placeholder-image.jpg" width="275px" height="183px" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
            <?php
				} 
			?>
   			</a>
    		<h3><span>Most viewed</span></h3>
    	</div>
    	<div id="popularpost-content">
        	<div id="popularpost-social">	
				<?php
                if(function_exists('display_social4i'))
                echo display_social4i("large","float-left");
                echo cust_excerpt(120)
                ?>
            </div>
       	</div>
        <div id="popularpost-meta">
        	<div class="recenttitle">
        		<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
            </div>
            <div class="recentauthor">
         		<small><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?> &bull; By <?php the_author_posts_link(); ?></small>
            </div>
        </div>
    </div>
<?php endwhile; wp_reset_query(); ?>


Hope that helps.

I did see that plugin before and it does seem to be a pretty good one, but as this section of the site is never changing i didn't really want to use a plugin if you know what i mean?
0

#8 User is online   brightonmike 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,996
  • Joined: 27-June 11
  • Reputation: 340
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 01 February 2012 - 10:59 AM

I've tried your exact code but no such luck. How strange. Tried moving the function around in my functions.php file.

I think I may just try the plugin, though I agree it's always best to try not to use loads of bloated plugins.
0

#9 User is online   MikeChipshop 

  • Small but imperfectly formed
  • Group: Moderators
  • Posts: 7,045
  • Joined: 19-April 10
  • Reputation: 503
  • Gender:Male
  • Location:Scotland

Posted 01 February 2012 - 11:21 AM

OK i just tried a stripped down version of the above on a different theme on a different server and it does indeed fail. It fails at the 'meta_key=post_views_count' section, so somewhere along the line i've obviously forgotten a step :/

I'll see if i can track down what i've forgotten.

EDIT!!!!
Doh! I totally forgot the actual tracking of the post!

Put...

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


anywhere in the loop for single posts

:blush1:

This post has been edited by MikeChipshop: 01 February 2012 - 11:24 AM

0

#10 User is online   brightonmike 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,996
  • Joined: 27-June 11
  • Reputation: 340
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 08 February 2012 - 02:18 PM

I still can't get it to work.

It just lists the posts in publish order!
0

#11 User is online   brightonmike 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,996
  • Joined: 27-June 11
  • Reputation: 340
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 09 February 2012 - 11:40 AM

Fixed. The issue is a conflict with the Sticky plugin.
0

#12 User is online   MikeChipshop 

  • Small but imperfectly formed
  • Group: Moderators
  • Posts: 7,045
  • Joined: 19-April 10
  • Reputation: 503
  • Gender:Male
  • Location:Scotland

Posted 09 February 2012 - 11:44 AM

Nice one mate. Strangely enough I'm having to fix someone's plugin at the moment that's having issues with Sticky posts. Not related just a funny co-incidence.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users