2009-07-02 22:17:36

alannie
Member
zêta
From: New York, USA
Known languages: en, ASL

Specify article_custom limit AND check for non-empty custom field?

I want to display on my home page a list of the three most recent articles that have a video associated with them. I’ve set up some custom fields related to the videos, and I’d like to be able to check for articles that have a non-empty “video_id” custom field.

My initial setup displayed no articles because the three most recent articles have empty video_id fields:

Page –

<txp:article_custom limit="3" form="video_list" >

Article form –

<txp:if_custom_field name="video_id">
[article list formatting here]
<txp:else />
</txp:if_custom_field>

And, I can’t just put the custom field attribute in the article_custom tag because its value will always change:

<txp:article_custom limit="3" form="video_list" video_id="???" />

Any ideas??

Offline

 

2009-07-02 23:08:44

artagesw
Developer
êta
Real name: Sam Weiss
From: Seattle, WA
Website

Re: Specify article_custom limit AND check for non-empty custom field?

Could you assign a “video” category to articles with video, and then filter by category perhaps?

Offline

 

2009-07-03 01:26:00

jsoo
Developer
omega
Real name: Jeff Soo
From: NC, USA
Known languages: English
Website

Re: Specify article_custom limit AND check for non-empty custom field?

Sounds like it ought to be a simple problem, and yet I can’t come up with a simple solution as you have stated it. artagesw’s solution is certainly simple if you are willing to use a category as suggested, and the minor duplication of effort that entails. Otherwise I think you’re looking at a plugin. I don’t know if any of the existing article_custom plugins do this out of the box (select on a custom field), but the mod shouldn’t be difficult.

Recently I’ve had a couple of occasions to use a different solution to the same kind of problem. (I learned it from net-carver.) It involves creating a temporary table with only the articles you want, letting article or article_custom use that, then discarding the temporary table. It’s actually quite a light-weight solution compared to a typical article_custom plugin, which is likely to involve duplicating a significant chunk of Txp code. I might even make a plugin along these lines…


Txp tags not doing what you expect? Learn to use a tag trace. And the Tag Reference.

Offline

 

2009-07-03 02:29:21

maniqui
Moderator
omega
Real name: Julián
From: Buenos Aires, Argentina
Known languages: es,en

Re: Specify article_custom limit AND check for non-empty custom field?

No doubt it would be very useful to grab all articles that have any value (!= empty) on a particular custom field…
It’s strange that Textpattern doesn’t seem to be able to do this easily.

Some kind of wildcard or flag (not sure if that’s the word) could make a simple solution. Like custom_field_name="txp:any" (or something like that).

@alannie

This may be a solution (not tested):

Page –

<txp:article_custom limit="3" form="video_list" sort="custom_3 desc, posted desc" >

Articles are sorted first by custom field (pick first those that have any value) and then by date.

Article form –

<txp:if_custom_field name="video_id">
[article list formatting here]
</txp:if_custom_field>

Inspired by this

BTW, smd_query plugin probably can fetch articles with any value on a particular custom field.

Last edited by maniqui (2009-07-03 02:42:56)


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

 

2009-07-03 03:20:51

Gocom
Developer
omega
Real name: Jukka Svahn
Website

Re: Specify article_custom limit AND check for non-empty custom field?

maniqui wrote:

This may be a solution (not tested):

Yep, ofcourse that works – or kinda. Reasons:

  • You can’t really use the limit.
  • Your sort just gets the videos with the lowest id/order by alphabet, not by date.

But you have to remember that this method is really slow compared to one single query. Not to mention that it will have to fetch all articles if you want to sort them by posting date and then parse and check every one of them – which will take for ever and ever.

One way to make maniqui’s code to work is to name the videos by the upload date which could arrange the videos correctly.

And the stupid way is to remove the limit, sort them by posted and…

<txp:article_custom limit="9999" sort="posted desc">
	<txp:if_custom_field name="video_id">
	</txp:if_custom_field>
</txp:article_custom>

…just wait the list to generate itself. Probably it will even kill the cheap shared host. The limit then could be done by using <txp:variable /> tags or by counting plugins. Which will make it even slower.

Or then you could do two seperate queries. Get the IDs and then serve them to the article_custom. For example:

<txp:php>
	$rs = safe_column('ID','textpattern',"custom_3 != '' ORDER BY Posted desc LIMIT 0, 3");
	if($rs) echo implode(',',$rs);
</txp:php>

But it’s too really dumb, because it a) uses two queries b) uses PHP c) still is slow.

Last edited by Gocom (2009-07-03 03:29:09)


Rah-plugins | What? I’m a little confused… again :-) <txp:is_god />

Offline

 

2009-07-03 05:19:47

colak
Admin
omega
Real name: Yiannis
From: Cyprus
Known languages: el, en
Website

Re: Specify article_custom limit AND check for non-empty custom field?

Would this work?

<txp:article_custom limit="3"  wraptag="ul" break="li">
<txp:if_custom_field name="video_id">
<txp:permlink><txp:title /></txp:permlink>
<txp:else />
</txp:if_custom_field>
</txp:article_custom>

Offline

 

2009-07-03 05:59:01

maniqui
Moderator
omega
Real name: Julián
From: Buenos Aires, Argentina
Known languages: es,en

Re: Specify article_custom limit AND check for non-empty custom field?

Hi colak,

that’s exactly the same (but just one difference: you did it using txp:article_custom as container tag) that alannie has already done (check her first post). Or am I missing something?


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

 

2009-07-03 06:00:02

MattD
Plugin Author
sigma
Real name: Matt Davis
From: Monterey, California
Website

Re: Specify article_custom limit AND check for non-empty custom field?

colak wrote:

Would this work?

<txp:article_custom limit="3"  wraptag="ul" break="li">
<txp:if_custom_field name="video_id">
<txp:permlink><txp:title /></txp:permlink>
<txp:else />
</txp:if_custom_field>
</txp:article_custom>

If one of the three articles didn’t have the field then only three would display.


My Plugins

In a way, each of us has an El Guapo to face. -Lucky Day (Three Amigos)

Offline

 

2009-07-03 08:40:36

colak
Admin
omega
Real name: Yiannis
From: Cyprus
Known languages: el, en
Website

Re: Specify article_custom limit AND check for non-empty custom field?

Hi Julián it is very much it and I cannot see why the original code alannie posted doesn’t work.

I guess what confuses me is <txp:article_custom limit="3" form="video_list" video_id="???" /> as I can not understand why video_id="???" is needed. The code I and alannie posted should only list the 3 latest articles whose custom field named video_id is populated.

Offline

 

2009-07-03 09:06:57

Gocom
Developer
omega
Real name: Jukka Svahn
Website

Re: Specify article_custom limit AND check for non-empty custom field?

colak wrote:

The code I and alannie posted should only list the 3 latest articles whose custom field named video_id is populated.

No. It lists 3 latest article by posting date and after that checks the custom field. It doesn’t in anyway filter them by custom field. Instead, it just doesn’t dislay anything if there isn’t the custom field set. So if the 3. recent articles don’t have video, then the code will return none.

So, it definetly won’t work. You can’t use the limit like that.

Last edited by Gocom (2009-07-03 09:08:40)


Rah-plugins | What? I’m a little confused… again :-) <txp:is_god />

Offline

 

Powered by FluxBB