Forums

popular posts - change order from comments count to views (11 posts)

  1. wildbug
    Member
    Posted 1 month ago #

    My theme is set up to show popular posts on the homepage. I just discovered that it is based on comments count, rather than views. I'd like to change it. The code used in the theme functions php is:

    # Displays a list of popular posts
    function fs_popular_posts($num, $pre='<li>', $suf='</li>', $excerpt=true) {
    	global $wpdb, $post;
    	$querystr = "SELECT $wpdb->posts.post_title, $wpdb->posts.comment_count, $wpdb->posts.ID, $wpdb->posts.post_excerpt FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ORDER BY $wpdb->posts.comment_count DESC LIMIT $num";
    	$myposts = $wpdb->get_results($querystr, OBJECT);
    	foreach($myposts as $post) {
    		echo $pre;
    		?>
    		<?php if (fs_settings('popular_thumb')=='enabled') : ?>
    		<?php fs_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?>
    		<?php endif; ?>
    		<a href="<?php echo get_permalink($post->ID); ?>"><?php the_title(); ?></a>
    		<p><?php echo fs_clean($post->post_excerpt, 140); ?>...</p>
    		<?php
    		echo $suf;
    	}
    }

    It seems to me all I have to do is change this line: "ORDER BY $wpdb->posts.comment_count DESC LIMIT $num"" but I"m not a programmer.

    Can someone tell me what to do to make my popular posts display based on how many views it got rather than # of comments? Thanks so much!!

  2. wildbug
    Member
    Posted 4 weeks ago #

    It seems like this should be a simple fix - a matter of exchanging a term (unknown to me right now) with "posts.comment_count." But I cannot find anything in the support forum.

    Can anyone help me? Thank you!

  3. wildbug
    Member
    Posted 3 weeks ago #

    Wow, I wish someone could help me fix this. Isn't it a simple fix??

  4. songdogtech
    Member
    Posted 3 weeks ago #

    Wow, have you searched in the forums for an answer or searched for a plugin? There is a plugin called Ozh' Click Counter « WordPress Plugins that will work, but you're going to have to add some php code yo your tempalte files.

  5. wildbug
    Member
    Posted 3 weeks ago #

    Thanks for responding. Yes I have searched and searched for info in the support forums and documentation. I looked for plugins. The one you linked to was for counting user clicks. I just want to know how to modify my existing layout so it counts user views instead of #of comments when determining what counts as a "popular post." I don't want a plugin...just to know what to replace

    ORDER BY $wpdb->posts.comment_count DESC LIMIT $num"

    with. Anyone?

  6. songdogtech
    Member
    Posted 3 weeks ago #

    What do you see as a fundamental difference between the two things: a page "click" and a page "view?"

  7. wildbug
    Member
    Posted 3 weeks ago #

    I have no idea. But it's sort of irrelevant to my problem. You linked me to a plugin. I already have the code in my theme for popular posts. I just need to change the way it is sorted.

  8. MichaelH
    moderator
    Posted 3 weeks ago #

    You need a plugin to count user views, WordPress doesn't do that natively. Once you have the plugin and can provide a link to that plugin, someone might be able to help you.

  9. songdogtech
    Member
    Posted 3 weeks ago #

    Wildbug, It's not irrelvent, because clicks and views are the same thing, and the plugin I pointed you to does that, if you read the docs on it.

  10. wildbug
    Member
    Posted 2 weeks ago #

    But I'm not looking for a plugin...I am looking to fix the code already on my theme. He didn't use a plugin, he provided the hard code. That is why I said it was irrelevant...I wasn't trying to be rude. If I use a plugin, then I have to remove his code, right? I just don't want to mess things up further...

  11. t31os_
    Member
    Posted 2 weeks ago #

    The point being, the comment count is stored in the database already, page views are not, it's not natively done in any of the code you have right now.

    It's not a simple few lines of code, else someone would have offered it up already..

Reply

You must log in to post.

About this Topic

Tags

No tags yet.