Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-05-28 05:48:30

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

count the number of articles

I’m trying to figure out how to count the number of live+sticky articles per section and the whole site.

I tried <txp:article_custom section='<txp:section />' status="live,sticky" pageby="1" pgonly /> and <txp:article_custom section="list,of,sections" status="live,sticky" pageby="1" pgonly /> but the sticky articles are not counted. Is there a vanilla way?


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

Offline

#2 2019-05-28 07:49:10

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,199
Website GitHub

Re: count the number of articles

Two ideas: what happens if you use just status with no value. If I’ve understood that correctly from other threads, omitting the value means it counts both (or all?) statuses, but I’m not sure if that applies just to txp:article and not to txp:article_custom:

<txp:article_custom section="list,of,sections" pageby="1" pgonly status />

I think using pgonly bypasses the default limit value of 10, but if not, you may need to set limit="9999" too.

The other way might be to do the article count for each status and add the result together using variables.

<txp:variable name="article_count" value='<txp:article_custom section="list,of,sections" pageby="1" pgonly status="live" />' />
<txp:variable name="article_count" add='<txp:article_custom section="list,of,sections" pageby="1" pgonly status="sticky" />' />
<!-- then output it where you need it -->
<txp:variable name="article_count" />

Do either/both of those work?


TXP Builders – finely-crafted code, design and txp

Online

#3 2019-05-28 10:01:05

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

Re: count the number of articles

^^ that did the trick! For learning purposes, I wonder if it will be less load on the server if the evaluate tag is used.


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

Offline

#4 2019-05-28 14:56:13

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,199
Website GitHub

Re: count the number of articles

colak wrote #318262:

^^ that did the trick!

Great! Just so we know: which of the two suggestions worked for you?

For learning purposes, I wonder if it will be less load on the server if the evaluate tag is used.

You may be onto something there. Oleg can probably say…?


TXP Builders – finely-crafted code, design and txp

Online

#5 2019-05-28 15:09:49

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

Re: count the number of articles

jakob wrote #318273:

Great! Just so we know: which of the two suggestions worked for you?

I implemented it in to ways here and here.


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

Offline

#6 2019-05-28 15:43:29

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,199
Website GitHub

Re: count the number of articles

Just a small minor-tweak: if you want to output the variable’s valuable immediately where you’ve defined or calculated it, you should be able to simply add output (or output="1") to the tag with the add attribute and drop the extra list item. That attribute was added in txp 4.7.2.


TXP Builders – finely-crafted code, design and txp

Online

#7 2019-05-28 16:07:19

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

Re: count the number of articles

Indeed. Implemented! Search for your name here :)


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 2019-05-29 05:06:26

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

Re: count the number of articles

<txp:article_custom section="list,of,sections" status pgonly pageby="1" />

should be fine. I don’t remember why we have not implemented status="live, sticky" too. Probably because it would be not bw-compatible (though the risk is minimal) and Stef had some further plans re status.

Offline

#9 2019-05-29 05:47:02

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

Re: count the number of articles

etc wrote #318282:

<txp:article_custom section="list,of,sections" status pgonly pageby="1" />...

should be fine. I don’t remember why we have not implemented status="live, sticky" too. Probably because it would be not bw-compatible (though the risk is minimal) and Stef had some further plans re status.

Hi Oleg,

Indeed it does although it is slightly counter-intuitive as it does not explicitly tell us that it will omit draft and pending statuses.


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

Offline

#10 2019-05-29 08:06:32

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

Re: count the number of articles

colak wrote #318283:

Indeed it does although it is slightly counter-intuitive as it does not explicitly tell us that it will omit draft and pending statuses.

The idea was to select all articles having a “visible” status, be it live, sticky or some future one. You can not output “hidden” statuses anyway, by privacy concern.

Offline

#11 2019-05-29 08:15:56

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,199
Website GitHub

Re: count the number of articles

etc wrote #318288:

The idea was to select all articles having a “visible” status, be it live, sticky or some future one. You can not output “hidden” statuses anyway, by privacy concern.

Excellent! And makes sense. Thanks for clarifying.


TXP Builders – finely-crafted code, design and txp

Online

#12 2020-07-15 11:02:10

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 465
Website

Re: count the number of articles

This works perfectly. Thank you very much!

One question:

How can I add a comma as a thousands separator for the resulting number?

“6,357 images” instead of “6357 images”


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#13 2020-07-15 12:00:35

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

Re: count the number of articles

Kjeld wrote #324536:

How can I add a comma as a thousands separator for the resulting number?

“6,357 images” instead of “6357 images”

Try adding escape="number" attribute to the outputting tag.

Offline

#14 2025-08-07 06:14:24

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

Re: count the number of articles

I’m returning to this as I was experimenting with the evaluate tag which did not work as expected and returned nothing when using the snippet below.

<txp:evaluate query='<txp:article_custom section="projects,about,blog,texts,publications" pageby="1" status="live" pgonly escape="number" /> + <txp:article_custom section="projects,about,blog,texts,publications" pageby="1" status="sticky" pgonly escape="number" />' />

I know that the above can be achieved in a simpler way and it is not a real life experiment.


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

Offline

#15 2025-08-07 07:00:51

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,199
Website GitHub

Re: count the number of articles

Try removing escape="number" from your tags. According to the post above that adds a comma, which is probably throwing off the addition.


TXP Builders – finely-crafted code, design and txp

Online

Board footer

Powered by FluxBB