Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-10-26 09:28:24

jdueck
Plugin Author
From: Minneapolis, MN
Registered: 2004-02-27
Posts: 147
Website

Return a list of categories sorted by recent content

Long-time textpattern user here but I’ve been stymied by this one.

How would I output a list of categories, sorted by which ones have the most recent articles in them?

txp:category_list doesn’t seem to support this.

I thought of doing a listing based on the following article form:

<txp:if_different>
<txp:category1 />
</txp:_if_different>

But then the question is how do I sort the list? If I sort by category I get all the categories but the category on top is not necessarily the on with the most recent article. If I sort by article date, I’m likely to get duplicates in the category list.

Is there a plugin which does this?

Offline

#2 2012-10-26 10:06:51

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,795
Website GitHub

Re: Return a list of categories sorted by recent content

jdueck wrote:

How would I output a list of categories, sorted by which ones have the most recent articles in them?

Hmmm, dammit. My immediate thought was to use <txp:recent_articles> as a container:

<txp:recent_articles>
   <txp:if_different>
      <txp:category1 />
   </txp:if_different>
</txp:recent_articles>

but that tag can’t be used as a container, nor does it have a form attribute. Is that an oversight do you think? I could fix it if so.

Failing that you can probably use etc_query or smd_query for now to do the SQL that txp:recent_articles does, then proceed as above. Uhhh, maybe.

Last edited by Bloke (2012-10-26 10:09:39)


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

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2012-10-26 12:25:22

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Return a list of categories sorted by recent content

What about a similar snippet as the one suggested by Bloke, but with article_custom?

<txp:article_custom>
   <txp:if_different>
      <txp:category1 />
   </txp:if_different>
</txp:article_custom>

La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#4 2012-10-26 13:51:55

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Return a list of categories sorted by recent content

Stef, recent_articles is the same as article_custom, minus features. Making the tag bit, well, useless.

Offline

#5 2012-10-26 17:05:37

jdueck
Plugin Author
From: Minneapolis, MN
Registered: 2004-02-27
Posts: 147
Website

Re: Return a list of categories sorted by recent content

maniqui wrote:

What about a similar snippet as the one suggested by Bloke, but with article_custom?

The reason this approach doesn’t work is because if_different only compares with the single previous iteration. It doesn’t test for uniqueness — only difference from the previous item in the list.

Let’s say I have these articles sorted by date:

  • Oct 6, 2012 – “How I Slop Pigs” (Category:Meaningful Labor)
  • Oct 5, 2012 – “Why I Love Bacon”(Category:Reciprocal Affection)
  • Oct 4, 2012 – “Breakfast Is Always Coming” (Category:Hope for the Future)
  • Oct 3, 2012 – “Potato Power” (Category:Meaningful Labor)

When I run the form above on articles sorted by date, it’ll do this:

Meaningful Labor
Reciprocal Affection
Hope for the Future
Meaningful Labor  <-- d'oh! duplicate!

Ah, but what if I sort by category, then by date? But then the output will be chronologically wrong, since it’s based on the alphabetical order of the categories rather than their dates:

Hope for the Future  <-- whoops: newest article in this category is two days older than the newest one
Meaningful Labor    <-- this category has the newest article, should be on top
Reciprocal Affection <-- this category has the second-newest article, should be 2nd

Offline

#6 2012-10-26 17:10:27

jdueck
Plugin Author
From: Minneapolis, MN
Registered: 2004-02-27
Posts: 147
Website

Re: Return a list of categories sorted by recent content

I think smd_query might be the way to go…I just need to figure out the SQL for something like “Select 10 distinct category names from the most recent 50 articles in descending order of article date.”

Offline

#7 2012-10-26 17:22:09

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

Re: Return a list of categories sorted by recent content

SELECT DISTINCT Category1 FROM textpattern ORDER BY Posted DESC nonsense

SELECT Category1 FROM textpattern GROUP BY Category1 ORDER BY MAX(Posted) DESC?

Last edited by etc (2012-10-26 18:06:31)

Offline

#8 2012-10-26 17:43:03

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

Re: Return a list of categories sorted by recent content

would this work?

<txp:variable name="recentcats" value='<txp:article_custom limit="50" break=","><txp:category /></txp:article_custom>'/>
<txp:article_custom wraptag="ul" break="li" limit="50" class="recent_categories">
<txp:rah_repeat offset="1" break="," duplicates="1" sort="regular asc" value='<txp:variable name="recentcats"/>'
<txp:rah_repeat_value />
</txp:rah_repeat>
</txp:article_custom>

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

Offline

#9 2012-10-26 18:12:18

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,795
Website GitHub

Re: Return a list of categories sorted by recent content

Gocom wrote:

recent_articles is the same as article_custom, minus features.

Ha! Good point. Never actually looked at the code. Should probably just be a stub for article_custom, or ditched.

@jdueck: Yeah, forgot about the duplicity, sorry. A solution along the lines of colak’s would have been my 2nd option after I’d realised the total failure of the first avenue.

Will try and engage brain before talking in future. You’d think I’d learn after all these years to keep quiet. Times like these reminds me of a phrase we always used at uni: “Stef, you’ve got nothing to say and you’re saying it too loud”.


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

Txp Builders – finely-crafted code, design and Txp

Offline

#10 2012-10-26 19:13:43

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

Re: Return a list of categories sorted by recent content

For me, smd_query (or etc_query) with

<txp:smd_query query="SELECT Category1 FROM textpattern GROUP BY Category1 ORDER BY MAX(Posted) DESC">
  {Category1}
</txp:smd_query>

is a clear winner over even <txp:article_custom limit="999"><txp:category1 /></txp:article_custom> sole, it is 10 times faster.

Offline

#11 2012-10-26 21:21:28

jdueck
Plugin Author
From: Minneapolis, MN
Registered: 2004-02-27
Posts: 147
Website

Re: Return a list of categories sorted by recent content

etc, I think thou hast hit it. I will experiment in the near future and report my findings.

Offline

#12 2012-10-28 22:04:12

jdueck
Plugin Author
From: Minneapolis, MN
Registered: 2004-02-27
Posts: 147
Website

Re: Return a list of categories sorted by recent content

OK so that works as far as it goes.

Now I need to restrict the results to categories that are children of a particular category. Is this feasible to do using SQL (or within textpattern) or is there a plugin that works?

I tried a few variations on the following but am getting blank results:

<txp:smd_query query="SELECT Category1 FROM `textpattern` WHERE 
  (Section='article' 
   AND Category1 != '' 
   AND FIND_IN_SET('<txp:category_list parent=writing exclude=writing break=, />',Category1)) 
   GROUP BY Category1 ORDER BY MAX(Posted) DESC">
       <txp:article_custom category="{Category1}" form="series_listing" 
       limit="1" pgonly="0" section="series" />
</txp:smd_query>

edit: I figured it out, best just to use SQL.

<txp:smd_query query="SELECT Category1 FROM `textpattern` WHERE 
  (Section='article' AND Category1 = ANY(SELECT name FROM `txp_category` WHERE parent = 'writing') ) 
   GROUP BY Category1 ORDER BY MAX(Posted) DESC">

<txp:article_custom category="{Category1}" form="series_listing" 
  limit="1" pgonly="0" section="series" />

</txp:smd_query>

Last edited by jdueck (2012-10-29 02:13:14)

Offline

Board footer

Powered by FluxBB