Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-12-09 19:51:24

Addison
Member
From: Flowood, Mississippi
Registered: 2004-08-30
Posts: 34
Website

Related and reverse-related article output

I have used custom fields so that I could have one-to-many relationships on a couple of client sites. It goes something like this:

<txp:if_custom_field name="Related Articles">
<txp:php>
$a = custom_field (array ('name' => 'Related Articles'));
$b = explode(',', $a);
// OUTPUT EACH RELATED ARTICLE
foreach ($b as $c) {
echo article_custom (array(
    'form' => 'related_articles_form',
    'id' => $c,
    'section' => 'section-name'
));
}
</txp:php>
</txp:if_custom_field>

In the main article custom field, you’d enter the related article IDs separated by commas (exactly like the Article Image field).

This has worked just fine in the past, but I’m approaching a rather large site and I’m concerned about performance. Has anyone else done anything like this, or maybe know a better way to handle it?

By the way, I’m not simply outputting a list of links to related articles — I’m showing the full content of related articles. For example, I may want to show some select testimonials (related articles) on a project page (main article).

Thanks,
Addison

Offline

#2 2009-12-09 20:29:54

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

Re: Related and reverse-related article output

Addison wrote:

In the main article custom field, you’d enter the related article IDs separated by commas (exactly like the Article Image field).

On large sites, not only is performance a potential problem, I’d guess finding the related article IDs in the sea of other articles is going to cause a headache for whoever is authoring the site.

Can you approach it with tags? Either with tru_tags if you’re not using keywords for anything else, or with smd_tags (which I could improve for you if required, though it should be able to handle this use case already).

For example, could you tag an article and then have your related articles automatically made up of other articles that share the same tag(s)? With some careful choice of tags, this might work (smd_tags has the ability to restrict tags to only those ones you have configured, or it can allow you to create them on-the-fly, a bit like tru_tags but in a more structured manner). The nested tag system might work to your advantage here, as could its ability to link tags to categories.

You may also be able to (ab)use soo_multidoc for this purpose, though it might be overkill or not quite the right fit in this case (I’ve never used it, but have heard great things about it).


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

#3 2009-12-09 20:42:28

Addison
Member
From: Flowood, Mississippi
Registered: 2004-08-30
Posts: 34
Website

Re: Related and reverse-related article output

Yes! It is a hassle to find those article IDs.

Your smd_tags plugin may well be the best (and easiest) solution — I hadn’t thought about using tags in that way. I’m downloading it now to play a little bit…

Thanks,
Addison

Offline

#4 2009-12-09 21:47:55

pieman
Member
From: Bristol, UK
Registered: 2005-09-22
Posts: 491
Website

Re: Related and reverse-related article output

Addison wrote:

Yes! It is a hassle to find those article IDs.

Addison, if you haven’t tried it, wet_quicklink can help with this.

Stef, I’m curious, why could performance be a problem with Addison’s current technique (one that I also use regularly)? To my tiny brain there seems to be little difference between retrieving articles by named ID or searching the articles table for keyword/category matches. I’d assume if anything the former would be more efficient than the latter. Apologies if I’m missing the point — I’m on a bit of a mission to get a better grasp of performance issues.

Offline

#5 2009-12-09 21:55:55

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Related and reverse-related article output

Bloke wrote:

For example, could you tag an article and then have your related articles automatically made up of other articles that share the same tag(s)? With some careful choice of tags, this might work (smd_tags has the ability to restrict tags to only those ones you have configured, or it can allow you to create them on-the-fly, a bit like tru_tags but in a more structured manner). The nested tag system might work to your advantage here, as could its ability to link tags to categories.

And for frontend output, you don’t need a plugin at all.

<txp:article_custom keywords='<txp:keywords />' />

Addison, to answer your original code, repeating/slipting, if you want to accomplish same stuff with TXP tag syntax, you can use rah_repeat for that purpose instead PHP.

Offline

#6 2009-12-09 22:25:20

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

Re: Related and reverse-related article output

pieman wrote:

why could performance be a problem with Addison’s current technique

Actually, it probably isn’t, though it might depend how it’s being used. On an individual article page it’s no problem since it’s only iterating over a defined set of article IDs. Thus it uses one query (article_custom) for each article in the Related Articles CF. Not too much overhead. On a list page (if the related info is being used, even in a condensed format) then the number of queries could grow quite rapidly and the page would slow down.

iirc, smd_tags grabs all related articles in one query and then ‘fakes’ article_custom by populating the necessary arrays, thus it’s far more efficient. I’d have to check that fact but I’m pretty sure that’s how I approached it.

So for a few articles there’s not much in it, but as the related list grows, or the page becomes more complicated the gap between an iterative approach vs a direct query approach widens. Practically, in this case, there’s probably little or no performance hit either way as you’re unlikely to show related info in a list context so you’re right that it’s rather a moot point here.

Does that kinda answer it?


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

#7 2009-12-09 23:43:59

pieman
Member
From: Bristol, UK
Registered: 2005-09-22
Posts: 491
Website

Re: Related and reverse-related article output

Bloke wrote:

On a list page (if the related info is being used, even in a condensed format) then the number of queries could grow quite rapidly and the page would slow down.

Simply because the article form is likely to be used more than once?

So for a few articles there’s not much in it, but as the related list grows, or the page becomes more complicated the gap between an iterative approach vs a direct query approach widens.

That echos what Gocom said in reply to my own performance-related ponderings earlier today. Now that I get the difference between the approaches you mention I’m wondering — rah_function, smd_query and smd_tags aside — how can I tell which plugins do their heavy lifting with a single SQL query, and equally which ones to be wary of?

Does that kinda answer it?

It does, yes. Pennies are dropping and clouds are slowly lifting. Thanks.

Addison, sorry for hijacking your thread. We seem to be looking for similar advice so I hope maybe some of this is useful to you. :)

Offline

#8 2009-12-09 23:56:29

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

Re: Related and reverse-related article output

pieman wrote:

Simply because the article form is likely to be used more than once?

Yup.

how can I tell which plugins do their heavy lifting with a single SQL query, and equally which ones to be wary of?

OT: Without looking at the code you can’t really tell how efficient a plugin is and, unless you start seeing page slowdowns (which would probably only occur after handover to a client and they’ve been hammering it a while), it’s not something you’d necessarily pick up on.

A rough indicator might be the HTML page output in Testing/Debugging modes. The number of queries and execution time can give quite a bit of info, backed up by the list of queries you see being executed in Debugging mode. If the total Queries: value goes up roughly linearly with the number of articles on the page then chances are you’ll get slowdowns and/or hit a ceiling on big-ass list pages.

Last edited by Bloke (2009-12-09 23:57:20)


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

#9 2009-12-10 04:26:21

Addison
Member
From: Flowood, Mississippi
Registered: 2004-08-30
Posts: 34
Website

Re: Related and reverse-related article output

No hijacking here — all good stuff.

I’m all ears.

-Addison

Offline

#10 2009-12-10 08:29:28

pieman
Member
From: Bristol, UK
Registered: 2005-09-22
Posts: 491
Website

Re: Related and reverse-related article output

Indeed, useful advice. Thanks Stef

Offline

Board footer

Powered by FluxBB