Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Filtering by custom field
Hi all ;)
I’m near to finish my latest project (a online book shop for a friend of mine) but I can’t figure how to filter articles by custom field.
I use a same page structure for different sections (“Catalog”, “eBooks”, “Upcoming Books”) and a custom field to store the book format (in print or digital). Mainly, all books will be available into the two formats.
I tried the match
attribute without any results. Please, could you give me some advice. Thanks by advance.
(I forgoten to precise the website is made with Textpattern, just because it a so cool CMS)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Filtering by custom field
Hi Patrick
You can use <txp:article cf="value" />
where cf
is the custom field name and value
is the content of the custom field in the write tab. The code filters articles that match that value. Needless to say that it works using article_custom
too.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Filtering by custom field
Hi Colak ;)
Sure. I tried. But I’m using the glz_custom plugin in order to offer to the client an easier way to choose by chekboxes. So the custom field stores that value print|ebook
.
I made a Regex for extracting the corresponding value regardless to the section where to display the articles (books) by format (print or ebook)…
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Filtering by custom field
Although I have never used glz_custom_fields, I always thought that its features should become part of the core.
Are your options either print OR ebook, or both?
Another untested way may be to use a variable or the evaluate tag, or both.
<txp:variable name="books" value='<txp:evaluate test=''<txp:custom_field name="name" />'' />' />
<txp:if_variable name="books" value="ebook">
<txp:article_custom limit="999">...</txp:article_custom>
</txp:if_variable>
<txp:if_variable name="books" value="print">
<txp:article_custom limit="999">...</txp:article_custom>
</txp:if_variable>
> edit important typo
Last edited by colak (2020-07-17 07:25:18)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Filtering by custom field
Thank you Colak.
I will try.
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Filtering by custom field
Yes, options are mainly both: print
and ebook
.
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Offline
Re: Filtering by custom field
I was waiting for your suggestion, Oleg. ☺️
Yeah! That seems to make the trick!
What a master you are!
Give me a little time to test on the real website in production.
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Filtering by custom field
It’s perfect, Oleg.
But… And I think I’m going to be crasy: I got any results for the ebooks
page (and I’m pretty sure, I need it)…
Here is some background:
Home page, “Catalog”, “Coming” and “eBooks” share the same default
page.
I use the glz_custom_field plugin (for simpler UX raisons within the back-office). The “Format” field is set as checkboxes with this two values (sorry, in French) : Broché
and Ebook
.
I use a TXP variable in order to store the format depending of the section (may be it’s a good idea?):
<txp:if_section name=",catalogue-general,prochainement"><txp:variable name="format" trim>Broché<txp:else />Ebook</txp:variable></txp:if_section>
And this is the magic (based on your previous advice):
<txp:if_article_list>
<txp:article break="div" breakclass="full table table-fixed flex mt0" breakby="3" searchall="0" sort="Posted asc" time='<txp:variable name="time" trim />' format='%<txp:variable name="format" />%' limit="3">
<txp::books_list />
</txp:article>
Where did I fail?
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Filtering by custom field
This won’t quite do what you expect:
<txp:if_section name=",catalogue-general,prochainement"><txp:variable name="format" trim>Broché<txp:else />Ebook</txp:variable></txp:if_section>
The reason is that the ‘else’ doesn’t set the variable, only the first bit (partially) does. If you stick your site in debug mode you’ll probably see an error.
This is better:
<txp:variable name="format" trim>
<txp:if_section name=",catalogue-general,prochainement">
Broché
<txp:else />
Ebook
</txp:if_section>
</txp:variable>
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
Re: Filtering by custom field
Thank you Stef.
I tried your advice (I now understand) but unfortunately, without any results.
I think it is due to the value of the custom field stored as a pair with a “pipe” delimiter: Broché|Ebook
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Offline