Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2025-10-23 08:04:40

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,666
GitHub Twitter

[Solved] Count a same occurrence into titles

I want to count my articles based on a single occurrence.

My article titles are all based on this format:

XXXX Yes OOOO
YYYY No RRR
ZZZ Yes MMMMM

So my goal is to count the number of times “Yes” occurs.

I tried using the fields attributes but without any results:

<txp:article_custom section="my-section" fields="(Title, LIKE'%<txp:variable name="string" />%')" limit="0">
...
</txp:article_custom>

Note: the TXP variable stores the string extracted from a REGEX.

I think there’s a solution. Could you help me, please?

Last edited by Pat64 (2025-10-23 16:06:51)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#2 2025-10-23 08:46:34

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

Re: [Solved] Count a same occurrence into titles

Hi Patrick,

If you just want to count articles, the simplest solution (assuming url_title follows the same format) is

<txp:article_custom url_title="% yes %" pgonly pageby="1" />

or

<txp:article_custom url_title='%<txp:variable name="string" />%' pgonly pageby="1" />

Offline

#3 2025-10-23 09:03:50

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,666
GitHub Twitter

Re: [Solved] Count a same occurrence into titles

I’m sorry, but that not works (nothing appears)…

Maybe because the string has two words (i.e. Yes Yes: First and last name)?

Last edited by Pat64 (2025-10-23 09:05:08)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#4 2025-10-23 09:15:35

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

Re: [Solved] Count a same occurrence into titles

Ah, ok, you should probably remove the surrounding spaces; url_title="%yes%", look at url_title of your articles.

Offline

#5 2025-10-23 09:23:33

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,666
GitHub Twitter

Re: [Solved] Count a same occurrence into titles

Yep.

I made this changes (but without any results…):

<txp:variable name="string" trim="/\s+/" replace='-' escape="lower" />
<txp:article_custom url_title='%<txp:variable name="string" />%' pgonly pageby="1" />

Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#6 2025-10-23 09:39:37

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

Re: [Solved] Count a same occurrence into titles

If you use 4.9, there was a silly bug that I have just patched (thanks to your question), so download txplib_publish.php again. In 4.8.8 it should work as is.

Offline

#7 2025-10-23 09:45:27

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,666
GitHub Twitter

Re: [Solved] Count a same occurrence into titles

Thanks a lot, Master 🙏

(I’ll try this afternoon)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#8 2025-10-23 20:17:36

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

Re: [Solved] Count a same occurrence into titles

Cool if it’s solved, but, if there is a need for grouping/counting articles by a substring (even regex) of a field, that’s easy to do. Any use?

Offline

#9 Yesterday 08:13:20

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,666
GitHub Twitter

Re: [Solved] Count a same occurrence into titles

Yep. It’s perfect! 👌
Thanks a lot, again.

In my case, this is for this page (verify if a book is available into the global catalogue and display a link to a search to retrieve all associated ones; otherwise, the results’ll be blank).

In action here (see the magnifying glass icon): https://editions-du-26-octobre.com/catalogue-general/nietzsche-et-l-ideal-aristocratique-raymond-espinose


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#10 Yesterday 10:24:02

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

Re: [Solved] Count a same occurrence into titles

I don’t really see why you need to check whether a title exists when you are already on this book’s page. If you want to know whether there are other books by this author, add exclude="id" to <txp:article_custom />?

Offline

#11 Yesterday 13:08:26

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,666
GitHub Twitter

Re: [Solved] Count a same occurrence into titles

Done. Thanks.

For your interest, that’s the code:

<txp:variable name="string" trim="/\s+/" replace='-' escape="lower"><txp:variable name="name" /></txp:variable>
<txp:variable name="regular" escape="integer" trim><txp:article_custom section="catalogue-general" exclude="id" url_title='%<txp:variable name="string" />%' time="past" pgonly pageby="1">
<txp:yield item="count" />
</txp:article_custom></txp:variable>
<txp:evaluate query='number(<txp:variable name="regular" />) > 0'><a href="<txp:site_url />catalogue-general/?q=<txp:variable name="name" trim="/(\s)/" replace='+' />" data-tooltip="<txp:if_variable name="visitor_lang" value="fr">Consulter tous les livres<txp:else />Discover all books</txp:if_variable>" class="tooltip tooltip-animate"><i class="icon svg va-middle" hidden style="margin-left:.5em"><svg aria-hidden="true" focusable="false" height="1em" width="1em" viewBox="0 0 16 16"><use href="#m-glass"></use></svg></i></a></txp:evaluate>

Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#12 Today 03:50:42

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,666
GitHub Twitter

Re: [Solved] Count a same occurrence into titles

Everything is working fine.

@Oleg:

My question is: could we limit the query to 2 (with the limit="2" attribute, which is sufficient in my case) instead of 0 to optimize the process?


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

Board footer

Powered by FluxBB