Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2008-07-21 02:40:20
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Matching both categories: cat1 AND cat2
I would like to list articles that match two categories – i.e. catA AND catB. This must be a perennial requirement but I haven’t found much in the forums about it.
The standard <txp:article_custom />
tag will allow a comma separated list of categories but the match that’s performed is: catA OR catB.
Just as an aside: is there any reason comma separated lists of sections and categories with <txp:article_custom />
is not documented? Both Textbook and the Textpattern Solutions book state that only a single section or category is allowed. I’d be happy to update Textbook with this hidden gem if it’s officially supported.
The obvious solution would be chh_article_custom
but it currently generates warning messages like there’s no tomorrow. I know setting the production status to live will hide these but it’s not really an acceptable solution. Also I’m a bit nervous to commit to this plugin when it might break even more in the future.
Another solution is to use a keyword instead of the second category, e.g.:
<txp:article_custom section="sec1" category="catA" keywords="catB" />
and make sure that all the required articles have a keyword of catB. Not particularly elegant, but it does work.
One other option might be to select all the articles in catA, e.g.
<txp:article_custom section="sec1" category="catA" />
and then filter them further in the article form:
<txp:if_article_category name="catB"> ... article belongs to catA AND catB ... </txp:if_article_category>
This can’t be very efficient – rather than getting MySQL to do all the work, half of it has to be done by Textpattern itself.
The final option might be to hack the Textpattern code. I believe the code that generates the MySQL query for filtering categories is in publish.php
. Somehow I would feel happier about this option, as long as it could be done in a safe and sensible way.
Are there any future plans to implement something like chh_article_custom's
category=“catA&catB” as standard or might there be any other options?
Thanks,
Adi
Offline
Re: Matching both categories: cat1 AND cat2
Yes, this is one of two reasons why I still use(d) chh_article_custom (the other being the parent-child category relationships it allows). In a test version of the recent pre-4.07 svn I could no longer get chh_article_custom to work at all and instead worked out an alternative solution using smd_query (needs the new txp:variable and parser so not in 4.06). I too would prefer a txp:article(_custom)-based solution.
TXP Builders – finely-crafted code, design and txp
Offline
#3 2008-07-21 16:12:26
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: Matching both categories: cat1 AND cat2
With the new parser you could also do this:
<txp:if_article_category number="1" name="catA">
<txp:if_article_category number="2" name="catB">
... article belongs to catA AND catB ...
</txp:if_article_category>
</txp:if_article_category>
Offline
Offline
#5 2008-07-21 19:15:12
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: Matching both categories: cat1 AND cat2
Gocom, I know :) But for those of us (that includes me) that are not fluent in MySQL, and if you don’t have too many articles, it’s not so bad I think…
Offline
#6 2008-07-22 00:46:38
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: Matching both categories: cat1 AND cat2
jakob wrote:
In a test version of the recent pre-4.07 svn I could no longer get chh_article_custom to work at all and instead worked out an alternative solution using smd_query (needs the new txp:variable and parser so not in 4.06). I too would prefer a txp:article(_custom)-based solution.
jakob, thanks. Yes, as wonderful as chh_article_custom
is, it is based on a duplication of the standard article/article_custom tag – so unless it’s continuously updated in parallel it will eventually become unusable.
smd_query
is an interesting option but I would probably keep it as a last resort.
Offline
#7 2008-07-22 01:01:24
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: Matching both categories: cat1 AND cat2
Els:
Thanks. I think 4.0.7 will be a great leap forward. But, although your suggestion would work, it would result in one database query and two lots of extra processing.
Gocom:
You’re right, database queries must be more efficient. I’m using the “one category, one keyword” solution at the moment, which results in one query and no extra processing. It works fine, but it offends my sense of elegance!
I’ve discovered that the option to specify a list of categories in article_custom was introduced in 4.0.6 (along with sections and authors). So you can list articles that, for instance, belong to (SectionA OR SectionB) AND (CatA or CatB) AND (AuthorA or AuthorB). It’s a great enhancement but the ANDs and the ORs are in the wrong place for my requirement!
Offline
#8 2008-07-22 08:46:06
- rsilletti
- Moderator
- From: Spokane WA
- Registered: 2004-04-28
- Posts: 707
Re: Matching both categories: cat1 AND cat2
I solved the problem this way, by nesting the conditionals.
function ras_if_article_category1($atts, $thing)
{
global $thisarticle;
assert_article();
extract(lAtts(array(
'name' => '',
),$atts));
return parse(EvalElse($thing, do_list($thisarticle['category1'] , $name)));
}
function ras_if_article_category2($atts, $thing)
{
global $thisarticle;
assert_article();
extract(lAtts(array(
'name' => '',
),$atts));
return parse(EvalElse($thing, do_list($thisarticle['category2'] , $name)));
}
the problem here is that it only effects display control and can throw off your pagination, it depends on what your using it for.
Offline
#9 2010-02-03 12:03:39
- klebiTxP
- New Member
- Registered: 2009-12-18
- Posts: 6
Re: Matching both categories: cat1 AND cat2
Hello Folks
I am using Textpattern · 4.0.6.
Is it possible to use if_article_category nested in if_article_category
like:
<txp:if_article_category number=“2” name=“test1”>
text
<txp:else />
<txp:if_article_category number=“2” name=“test2”>
text
<txp:else />
text
</txp:if_article_category>
</txp:if_article_category>
somehow it’s not working. is there another solution for me?
Offline
Re: Matching both categories: cat1 AND cat2
klebiTxP wrote:
somehow it’s not working. is there another solution for me?
Update. The new tag parser can do many-many things more and better than the old used in 4.0.6.
Future posts go here, to the new topic you created klebiTxP. Do not post to this thread about this matter.
Offline
#11 2010-02-03 12:25:37
- klebiTxP
- New Member
- Registered: 2009-12-18
- Posts: 6
Re: Matching both categories: cat1 AND cat2
thanks for your answer. Maybe you are right. There is no particular reason for not updating txp.
For now I would like to stick with our installation.
Offline