Loading any Object (Entry, Page...) Based on a Custom Field Value

| No Comments | No TrackBacks |
Did you know that 'search_by_meta' is a little known method of the MT::Object class?  Until today, I didn't, but I came accros this  hidden gem today while writing a custom Perl script for one of my Movable Type installations.  Basically I needed to pull one specific page from the database based on a value that was stored in a custom field.  It seemed easy enough, and at first I tried the standard 'load' method...
Basically I tried this:

(my $page) = MT::Page->load({field.field_name => "somevalue"});

As I already suspected, this did not work.  After some searching with Google I came accross this little forum posting, which held the key.  This code works:

my $mt = MT->instance();
(my $page) = MT::Page->search_by_meta('field.field_name', 'somevalue');

Shame the documentation for this incredibly useful method is hidden in an obscure part of the documentation that is not easily findable through Google.

No TrackBacks

TrackBack URL: https://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/31

Leave a comment