Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-07-17 05:52:18

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

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

#2 2020-07-17 06:01:02

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

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

#3 2020-07-17 06:13:59

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

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

#4 2020-07-17 07:11:12

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

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

#5 2020-07-17 07:29:12

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

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

#6 2020-07-17 07:51:29

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

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

#7 2020-07-17 08:06:18

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

Re: Filtering by custom field

Pat64 wrote #324612:

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.

Hi Patrick, would <txp:article format="%print%" /> work?

Offline

#8 2020-07-17 08:17:34

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

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

#9 2020-07-17 09:11:22

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

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

#10 2020-07-17 09:26:18

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

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

#11 2020-07-17 10:01:15

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

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

#12 2020-07-17 10:12:41

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

Re: Filtering by custom field

Pat64 wrote #324625:

I think it is due to the value of the custom field stored as a pair with a “pipe” delimiter: Broché|Ebook

This shouldn’t be a problem. But I’m not sure the plugin stores these values in textpattern table, you should check.

Offline

Board footer

Powered by FluxBB