Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-01-12 06:58:45

capatxp
Member
Registered: 2013-01-12
Posts: 21

weighting stories for news site

I’m building a news website.

On the front page right now, it’s just listing articles from newest to oldest, regardless of how IMPORTANT the story might be. (so a BIG NEWS story could wind up being the third link down on the front page, below two minor stories, which would be a bad thing.)

So I’m thinking … mebbe you folks could point me in the right direction so that I can:

1) primarily list stories by day published, from newest to oldest (I guess this already takes place, so it’s already done) and
2) secondarily lists stories by WEIGHT, so a story flagged “important!” that day goes higher than one that is not.

The news site will be updated weekly, let’s say, so a story flagged “important” from last week should be listed below a non-important story from this week.

So for the first week the stories might be listed in this order:

Downtown explodes in giant ball of flame (flagged “important”; published that day at 5 p.m.)
Cute kittens love to drink milk! (published same day at 7 p.m.) (newer story, but ranked lower)

then next week:
City Council votes to annex Canada (flagged “important”)
High school sports team loses game.
Downtown explodes in giant ball of flame
Cute kittens love to drink milk!

Offline

#2 2013-01-12 10:30:19

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: weighting stories for news site

It looks like your site is issue-based (one per week). The easiest way is to introduce two custom fields: issue (say, custom_1) and weight (custom_2) that will be assigned by the publisher. Then just call <txp:article /> with sort="custom_1+0" DESC, custom_2+0 DESC, LastMod DESC" attribute (not tested but should work).

If you prefer more linear date-based structure, it’s more tricky, but feasible too. For example, if weight is still stored in custom_2 field, the following

<txp:article sort="ADDDATE(Posted, custom_2) DESC" />

will keep important articles on the top of the list for (roughly) custom_2 days.

Last edited by etc (2013-01-12 16:32:29)

Offline

#3 2013-01-12 20:16:21

capatxp
Member
Registered: 2013-01-12
Posts: 21

Re: weighting stories for news site

Thanks etc :)

I shall try that.

Offline

#4 2013-01-13 00:46:32

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: weighting stories for news site

One thing to note if using the sort="custom_1+0" trick – I’ve used this here and there on some sites and it works a treat, but within an individual article, the link_to_next and link_to_prev don’t work as they sort by date.

I’ve found its ok with older and newer though, so in article list view paging should be fine.

Offline

#5 2013-01-13 02:02:41

capatxp
Member
Registered: 2013-01-12
Posts: 21

Re: weighting stories for news site

One easy way to do what I want is to simply set the “Published at” time on the article’s word processing page to a later time for more important stories.

So an important story gets set to 11 p.m. while a minor story gets set to 10 p.m.

What do you think of this method?

Alternately, is there a way to do a sort where the date (with no time stamp, JUST THE DATE) is sorted first, followed by the custom_1 “weight” flag? The goal being listing all stories from a certain day by weight:

So it’ll list ‘em in this order:

Jan 3 story IMPORTANT
Jan 3 story minor
Jan 2 story IMPORTANT
Jan 2 story IMPORTANT
Jan 2 story minor

Offline

#6 2013-01-13 02:14:18

capatxp
Member
Registered: 2013-01-12
Posts: 21

Re: weighting stories for news site

I’ve got it working on my test site http://www.bozemanfreepress.com/txp/

(just look at the home page; ignore other sections as they’re messed up right now)

but I’m doing something a bit hackish in that I’ve set all the “Published at” timestamps to 12:00:00.

I’m using this tag: <txp:article limit=“5” sort=“Posted desc, custom_1 asc”/>

Is there a clean way to do this so I don’t have to set all the timestamps to 12:00:00?

Offline

#7 2013-01-13 07:46:21

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,386
Website GitHub Mastodon Twitter

Re: weighting stories for news site

is there a reason you are not just using <txp:article limit="5" sort="custom_1 asc, Posted"/>. I think that this will not need to change the timestamp.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#8 2013-01-13 14:56:57

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: weighting stories for news site

capatxp wrote:

is there a way to do a sort where the date (with no time stamp, JUST THE DATE) is sorted first, followed by the custom_1 “weight” flag?

<txp:article sort="DATE(Posted) DESC, custom_1 ASC" /> should do it.

Offline

#9 2013-01-13 19:34:58

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: weighting stories for news site

jstubbs wrote:

One thing to note if using the sort="custom_1+0" trick – I’ve used this here and there on some sites and it works a treat, but within an individual article, the link_to_next and link_to_prev don’t work as they sort by date.

You are right, Jonathan, link_to_next will not work with sort="custom_1+0 DESC, custom_2+0 DESC, LastMod DESC", but the reason (as I get it) is not custom_1+0, but multiple sort axes. For the same reason, it will fail with sort="DATE(Posted) DESC, custom_1 ASC". But I have seen a tip that can be used here if needed.

Offline

#10 2013-01-13 21:28:38

capatxp
Member
Registered: 2013-01-12
Posts: 21

Re: weighting stories for news site

That works :)

thanks!

etc wrote:

capatxp wrote:

is there a way to do a sort where the date (with no time stamp, JUST THE DATE) is sorted first, followed by the custom_1 “weight” flag?

<txp:article sort="DATE(Posted) DESC, custom_1 ASC" /> should do it.

Offline

#11 2013-01-14 00:53:40

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: weighting stories for news site

etc wrote:

You are right, Jonathan, link_to_next will not work with sort="custom_1+0 DESC, custom_2+0 DESC, LastMod DESC", but the reason (as I get it) is not custom_1+0, but multiple sort axes. For the same reason, it will fail with sort="DATE(Posted) DESC, custom_1 ASC". But I have seen a tip that can be used here if needed.

I just tried this two days ago on a site I was developing – but only with <txp:article limit="999" sort="custom_2+0"> i.e just one sort method. That works fine with older and newer but not link_to_prev or link_to_next. So I’m not sure it fails due to multiple sorting?

Offline

#12 2013-01-14 08:44:16

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: weighting stories for news site

jstubbs wrote:

I just tried this two days ago on a site I was developing – but only with <txp:article limit="999" sort="custom_2+0"> i.e just one sort method. That works fine with older and newer but not link_to_prev or link_to_next. So I’m not sure it fails due to multiple sorting?

Multiple sorting will fail, but you are right again, it does not even switch to Posted desc with sort="custom_2+0", but gives something weird. Now that I look at the code again, this is to be expected, so one should probably mention it in the documentation? And probably modify getPrevNext function to treat complex constructions like this one.

Offline

#13 2013-01-14 10:38:08

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: weighting stories for news site

You are probably right Oleg that it would be a good idea to mention this to the devs, but maybe someone with more advanced PHP skills like yourself could discuss the issues in the docs? Unless it deserve to be listed as a new issue?

Offline

#14 2013-01-14 15:50:20

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: weighting stories for news site

That’s not a real issue, just an overlook. I don’t know how to submit proposals, sorry if this is not the appropriate place, but I would replace

if (empty($m[0]) || count($m) > 2 || preg_match('/[),]/', $m[0]))

in getNextPrev function by something like

if (empty($m[0]) || count($m) > 2 || !preg_match('/^(?:[0-9,a-z,A-Z$_\x{0080}-\x{FFFF}]+|`[\x{0001}-\x{FFFF}]+`)$/u', $m[0]))

I will also try another solution that respects any sort order and report back if the performance is decent.

Offline

#15 2013-01-14 16:56:01

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,458
Website GitHub

Re: weighting stories for news site

etc wrote:

I don’t know how to submit proposals

If your testing works out and you think your patch is useful, you can raise an issue which will give us good visibility of it and we can track its progress. Patches are welcome but if you’re not comfortable with a diff tool, you can just outline the issues and fix as you have done here. Feel free to link to forum posts/this thread from your issue, to serve as background reading.

Last edited by Bloke (2013-01-14 16:56:33)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Online

Board footer

Powered by FluxBB