Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2009-07-02 22:17:36
- alannie
- Member
- From: Minnesota, USA
- Registered: 2005-09-15
- Posts: 150
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
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
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…
Code is topiary
Offline
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)
Offline
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)
Offline
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>
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
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?
Offline
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.
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
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.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
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)
Offline
Re: Specify article_custom limit AND check for non-empty custom field?
Gocom’s right, it pulls out a maximum of 3 fields and then looks at the custom field contents. If all of them happen to have video_id set, you’re good to go. If not, you’ll see fewer articles.
artagesw’s solution is by far the simplest, but if you’re already using the categories for other things you’re stuck with Gocom’s “grab all articles and filter using txp:variables/smd_if”, some PHP or a one-stop smd_query. fwiw, off the top of my head, this should work:
<txp:smd_query query="SELECT * FROM textpattern WHERE video_id != '' ORDER BY Posted desc LIMIT 3">
<a href="/{Section}/{url_title}">{Title}</a>
</txp:smd_query>
EDIT: fix braindead 1st attempt
Last edited by Bloke (2009-07-03 09:35:15)
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
#12 2009-07-03 12:19:49
- alannie
- Member
- From: Minnesota, USA
- Registered: 2005-09-15
- Posts: 150
Re: Specify article_custom limit AND check for non-empty custom field?
Thanks everybody for your feedback. Manqui, I had the same thought you did but then realized the videos have different and totally unrelated ids. They are posted on different services (google, youtube, etc.) and I’m using the the_video
plugin to embed them. If they were all on the same service and ids were assigned consecutively, conceivably I’d then simply be able to sort by id. But alas, that is not the case.
I was hoping to avoid having to assign a category, but it looks like this may be the only way for now (that doesn’t bog down the site, as Gocom pointed out). Thanks all!
Offline