Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-10-04 22:58:48

nemozob
Member
Registered: 2008-04-15
Posts: 36

Loop through articles of a given section, find custom field match

For the sake of simplicity I have two sections: ‘people’ and ‘cars’.

When I create an article in the ‘people’ section I assign the ID of a previous ‘cars’ article in first custom field (I’m calling ‘cars_field’) of the new ‘people’ article. The idea is that when I view a ‘people’ article I can, using PHP, grab the ID that appears in the ‘cars_field’ and then output the ‘cars’ article that matches that ID. It looks something like this:

$cars = custom_field (array(‘cars_field’ => ‘venue’,));
echo ‘<txp:article_custom id=”’.$cars.’” form=“single” />’;

What I’d like to do now is the reverse if I’m showing a ‘cars’ article but without having to do any more data entry. So it would look something like this with PHP:

- Loop through all articles in the ‘people’ section only
- In each ‘people’ article look at its ‘cars_field’ and see if it matches the ID of the ‘cars’ article I’m viewing
- If there’s a match output that ‘people’ article

Any thoughts? I’m basically trying to establish a two-way relationship between two articles. It’s not super efficient but for my purposes this will work.

Offline

#2 2008-10-04 23:17:14

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Loop through articles of a given section, find custom field match

nemozob wrote:

The idea is that when I view a ‘people’ article I can, using PHP, grab the ID that appears in the ‘cars_field’ and then output the ‘cars’ article that matches that ID. It looks something like this:

This can also be done with article_custom and asy_wondertag:

<txp:asy_wondertag>
<txp:article_custom id="<txp:custom_field name="cars_field" />" form="single" />
</txp:asy_wondertag>

What I’d like to do now is the reverse if I’m showing a ‘cars’ article but without having to do any more data entry.

<txp:asy_wondertag>
<txp:article_custom section="people" cars_field="<txp:article_id />" form="single" />
</txp:asy_wondertag>

(In the upcoming release 4.0.7 you even won’t need asy_wondertag anymore to use tags as attributes.)

Offline

#3 2008-10-05 00:44:52

nemozob
Member
Registered: 2008-04-15
Posts: 36

Re: Loop through articles of a given section, find custom field match

Okay, great. Yes, this seems to be working. One other question: What if I want to assign more than one ID to cars_field? So that in the case of:

<txp:asy_wondertag>
<txp:article_custom id=”<txp:custom_field name=“cars_field” />” form=“single” />
</txp:asy_wondertag>

‘cars_field’ looks like: 2,5,7

I have been using some PHP to explode the string to an array and loop through that but is there a way to do this with the asy_wondertag?

Offline

#4 2008-10-05 11:39:23

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: Loop through articles of a given section, find custom field match

nemozob wrote:

… is there a way to do this with the asy_wondertag?

If I’m not wrong the “article_custom” tag accepts a comma separated list of id so the code given by Els should work also in this case.

Offline

#5 2008-10-05 12:46:57

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Loop through articles of a given section, find custom field match

redbot wrote:

If I’m not wrong the “article_custom” tag accepts a comma separated list of id

I don’t think so (comma separated lists allowed only for section, category and author). But it’s probably something that can be done in 4.0.7 with the txp:variable tag. Until then, you’re stuck with your PHP I think ;)

Offline

#6 2008-10-05 13:41:05

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: Loop through articles of a given section, find custom field match

Els wrote:

I don’t think so (comma separated lists allowed only for section, category and author). But it’s probably something that can be done in 4.0.7 with the txp:variable tag. Until then, you’re stuck with your PHP I think ;)

Argh! I’m wrong again – I should stop answering questions without first testing them.
Anyway – I have no time to elaborate now but I’m sure you can achieve what you want with the wonderful plugin smd_each (remember to set the attribute subset=“2”).
Hope at least this helps.

Last edited by redbot (2008-10-05 13:42:08)

Offline

#7 2008-10-05 13:55:47

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Loop through articles of a given section, find custom field match

Els wrote:

I don’t think so (comma separated lists allowed only for section, category and author).

One of my wishes that ruud made come true for TxP 4.0.7. :D


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#8 2008-10-05 14:32:00

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Loop through articles of a given section, find custom field match

maniqui wrote:

One of my wishes that ruud made come true for TxP 4.0.7. :D

Ah, I missed that one! Good on you! Now all we have to do is wait for 4.0.7 :)

Offline

Board footer

Powered by FluxBB