Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[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
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
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
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
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
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
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
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
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
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
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
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