Getting Findfields to work
August 5, 2012 at 10:20 am #168127
I have been strugeling with the following:
I am migrating a simple php website to a wordpress driven website.
I have 2 simple pods; one called “vessels” and one called “services”
In the vessels pod I have a few simple fields and one of that is a picked field so I can pick a service from the other pod called services.
fields are: (name (txt) ,slug (slug), service (pick service) ,picture (file) ,teu (txt) ,callsign (txt) ,imo (num) ,length (num) ,grosstonagge (num) ,nettonagge (num) , monday (txt) ,tuesday (txt) ,wednesday (txt) ,thursday (txt) ,friday (txt) ,saturday (txt) ,sunday (txt) ,inactivefleet (bool)
In the services pod I only have a name field. (values are : UK, Ireland, France)
So far so good.
Now I want to display a page with all the vessels and a page with the sailing schedule per vessel in a table.
The vessels page is working fine.
On the services page I want to show a table with the following fields sorted by service(name) and only if it is in the activefleet (boolean)
VesselName Monday Tuesday Wednesday Thursday Friday Saturday Sunday
So in this case it would need to show 3 sets of rows each separated by service(name)
I have this setup on the old website here (http://www.bgfreightline.com/services.php)
How can I get this in a findrecords query?
I have this :
$vessels = new Pod(‘vessels’);
$vessels->findRecords($orderby = ‘service.name DESC’);
$total_vessels = $vessels->getTotalRows();
this will show me all the records. I need 3 sets sorted by service??
Anyone knows how to get this done?
RichardAugust 5, 2012 at 2:18 pm #168128
The latest 1.x versions of Pods support passing a parameter array both for findRecords() and when creating a new Pod object. It’s a handy shortcut when creating your pod object when you know you’re going to be doing specific sorting and filtering up front, see the second example shown at the top of the findRecords() docs. The parameter array works the same for creating a new pod object or using findRecords, and the docs for findRecords lists the supported parameters.
Note that I’m sorting by the service name first (descending) then by vessel name, secondarily. That should return the records in the same order as the example you linked. The ‘where’ parameter requires your ‘inactivefleet’ Boolean to be set, filtering out vessels that aren’t checked. Setting ‘limit’ to -1 ensures you get all specified records returned (you’ll note in the docs that the default for limit is 15). An extra variable to track when the service name changes so you can insert a new service row into the table and that’s pretty much it except the markup and get_field() calls.
I can’t guarantee it’s 100% working or even free from syntax errors since I whipped it up quickly without any actual testing, but it should be good enough to give the idea.August 6, 2012 at 4:58 am #168133
Thanks alot PG Lewis,
I will give that a go when i’m back home.
I just don’t understand when to use the .t option and when to use the podname.name option??
English is not my nativelanguage so its a bit hard to get my head around this consept…
I’ll let you know if I can get this to work for me.
RichardAugust 6, 2012 at 2:08 pm #168134August 6, 2012 at 4:16 pm #168137
In your second code example, you are grabbing all the get_field values and stuffing them into variables outside of the fetchRecord() loop. The vessel information will obviously change for each record, so you need to do this AFTER your while loop. Otherwise, I believe it looks close at a glance.August 6, 2012 at 4:24 pm #168138
The ‘t.’ prefix is an alias used to access any direct fields in the underlying Pod table… in this case, any direct fields in the ‘vessel’ Pod.
If you want to get individual values from a pick field, you use the pick field name followed by the related Pod field’s machine name:
get_field(‘service.name’) // From a vessel record, gets the name field for the related service
If you have more complicated relationships, this syntax will work several levels deep (not well documented, but VERY handy!)
get_field(‘parent.child.anotherlevel.ID) // No need to create more Pod objects to get a single value multiple levels down
It’s also handy for directly getting to the image URL from a file upload field:
get_field(‘image_field.guid’);August 7, 2012 at 3:51 am #168139
Thanks for your great support in this.
I know I would not have come up with that piece of code…;-)
You are brilliant man!!
It works now.
I did what you suggested and moved the setting variables code block right after the While loop.
Also thanks for clearifying the t.prefix methode.
Like you said its in the documentation but I a more visual kind a guy…I need to see it in action in a video for example.
You must be logged in to reply to this topic.