Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-01-01 19:47:22

cabes
Member
Registered: 2008-01-01
Posts: 28

Sorting articles by hits?

Hi,
is there a way to display a list of articles sorted by the number of hits? Like maybe pull info from the log file or something? I found a plugin a while back but it didnt seem to work for the new versions of textpattern.
Thanks,
Caleb

Offline

#2 2008-01-02 12:55:03

guiguibonbon
Member
Registered: 2006-02-20
Posts: 296

Re: Sorting articles by hits?

Set the name of one of your custom fields to hits, in the advanced preferences.

Then, in the article form, put this :

<txp:php>
if ($thisarticle["hits"] == "") $thisarticle["hits"] = 0;
$hits = doSlash($thisarticle["hits"])+1;
$ID = $thisarticle['thisid'];
safe_update("textpattern", "custom_2 = '".$hits."'", "ID = $ID"); //change custom_2 to wathever custom field you named hits
</txp:php>

To sort your articles by the number of hits use sort="custom_2 desc" in the article tag. Again, change custom_2 to wathever custom field you named hits.

Offline

#3 2008-01-02 13:22:54

cabes
Member
Registered: 2008-01-01
Posts: 28

Re: Sorting articles by hits?

Nice!

Thanks so much, this will be awesome.

Offline

#4 2008-01-02 22:29:03

davidsess
Member
From: New York, NY
Registered: 2008-01-02
Posts: 20
Website

Re: Sorting articles by hits?

Thanks so much for this. I almost have it working, but it’s just generating a random list of articles, as opposed to a properly ranked list. Does it matter which form I stick the PHP in?


http://www.patrolmag.com

Offline

#5 2008-01-03 08:59:16

guiguibonbon
Member
Registered: 2006-02-20
Posts: 296

Re: Sorting articles by hits?

Articles which have no hits or an equal number of hits will be sorted randomly. If you rather have those sorted by post time amongst them self use sort="custom_2 desc, posted desc".

The PHP basically just says “one more hit for the article i’m currently displaying”. It’s definitely recommended to only use it in the form used to display individual articles (in the archive), and not in article lists. Otherwise it will dramatically increase the number of queries on the page (and add undesirable hits). Or just wrap it with an <txp:if_individual_article></txp:if_individual_article> tag.

Last edited by guiguibonbon (2008-01-03 14:22:16)

Offline

#6 2008-01-03 13:07:09

net-carver
Archived Plugin Author
Registered: 2006-03-08
Posts: 1,648

Re: Sorting articles by hits?

guiguibonbon

I like your solution to this problem. Thanks for posting it.


Steve

Offline

Board footer

Powered by FluxBB