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,397
Website

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: 8,159
Website

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.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
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,397
Website

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: 8,159
Website

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.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
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,397
Website

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,397
Website

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: 3,768
Website

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,397
Website

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,397
Website

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: 9,637
Website

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

Board footer

Powered by FluxBB