See https://developer.wordpress.org/reference/functions/get_page_by_title/, then instantiate the pods object with the post ID if a WP_Post object is returned. The second argument can be an ID. See https://docs.pods.io/code/pods/#Get_the_Pods_object_for_a_specific_ID_or_slug
Or try $pod = pods( 'plant', sanitize_title( $BotanicalName ), true );
as the second argument can be an ID or a slug.
Otherwise, if you really must do a find
with a where
when two core functions are available, it looks like t.name
might work if you instantiate the pod with pod name only and then run ->find( $params )
based on this example.
@pdclark Thanks for the workarounds but as usual the documentation doesn’t generally reflect what people say. I have already implemented a workaround, but I’ll report this as a bug as I don’t think I’m doing anything wrong.
Is there a glossary of all the pods terms with a mapping to how the object looks anywhere?
The error you’re making is passing an array of params to the id_or_slug parameter. The linked doc in the previous post shows params go to find().
Also name, not post_title, also shown in the doc.
They’re not “workarounds” — they’re three correct solutions. What you shared is incorrect in multiple ways if you read the examples.
@pdclark At “https://docs.pods.io/code/pods/#Get_the_Pods_object_for_a_specific_ID_or_slug” it has this example:
$params = [
‘orderby’ => ‘t.name DESC’,
‘limit’ => 25,
‘where’ => ‘t.name != “Buster Keaton”‘,
];
$mypod = pods( ‘mypod’, $params );
If it was wrong it should also fail, not continue and in strict mode & “find it”.
-
This reply was modified 1 year, 2 months ago by dbareis.
Hey there @dbareis, the exists() method is meant for ID/slug lookups. For find(), you can use a conditional check that’s slightly different.
You could check if ( $pod->fetch() )
before you run your logic for a single item. If you want a more specific response then you can check if $pod->total()
is 1.
@sc0ttkclark @pdclark
Sorry but I’m getting lost, what I’m trying to do is find and update OR CREATE an object, the “key” (post title) is unique. Because the “save” documentation incorrectly says that it will update or create an object I’m doing that but it never updates.
Hence me trying to find an object with that specific key above. I can find an object but update never works so I need a writeable object that can be updated and saved. I have tried many different ways not just the above….
I’m importing data from static pages (i.e. not PODS data). I wrote the export. I have all the other PODS stuff working, just the import. I can live with this problem by deleting plants before import but there are other low priority issues that this would resolve.
It doesn’t help that there is no single page listing all the pods functions, breaking everything up into small pages makes the documentation almost impossible to use. If you can point me to a complete example reading and updating a pod that would be great, thanks.
@dbareis This is what ended up working for me. For some reason the DB didn’t like t.name
as in the docs @sc0ttkclark , but t.post_title
worked. I also had trouble with if ( $pod->fetch( $params )
, and ->total()
after a fetch, but the below method worked for saving and updating:
<?php
$plants = [
'Annona cherimola',
'Annona muricata',
'Asimina triloba',
'Blighia sapida',
'Cornus sanguinea',
'Durio',
'Euterpe precatoria',
'Mangifera',
];
foreach( $plants as $botanical_name ) {
$params = [
'where' => sprintf( 't.post_title = "%s"', $botanical_name ),
'limit' => 1,
];
error_log( '$params for find(): ' . print_r( $params, true ) );
$pod = pods( 'plant', $params );
if ( 1 === $pod->total() ) {
$pod->save(
[
'post_content' => sprintf( 'Updated at %s', date( 'Y-m-d h:i:s' ) ),
],
null,
$pod->field('ID')
);
}else {
$pod->save([
'post_title' => $botanical_name,
'post_content' => sprintf( 'Created at %s', date( 'Y-m-d h:i:s' ) ),
'post_status' => 'publish',
]);
}
}
It should be noted in the above example and original, if an apostrophe or quotation mark were to make its way into a botanical name, things would break. The solution to that is to sanitize the value by using post_name
and sanitize_title()
, as shown below:
<?php
$plants = [
'Annona cherimola',
'Annona muricata',
'Asimina triloba',
'Blighia sapida',
'Cornus sanguinea',
'Durio',
'Euterpe precatoria',
'Mangifera',
];
foreach( $plants as $botanical_name ) {
$params = [
'where' => sprintf( 't.post_name = "%s"', sanitize_title( $botanical_name ) ),
'limit' => 1,
];
error_log( '$params for find(): ' . print_r( $params, true ) );
$pod = pods( 'plant', $params );
if ( 1 === $pod->total() ) {
$pod->save(
[
'post_content' => sprintf( 'Updated at %s', date( 'Y-m-d h:i:s' ) ),
],
null,
$pod->field('ID')
);
}else {
$pod->save([
'post_title' => $botanical_name,
'post_content' => sprintf( 'Created at %s', date( 'Y-m-d h:i:s' ) ),
'post_status' => 'publish',
]);
}
}
@pdclark Thanks for the example code. As you found “t.post_title” works but the name field doesn’t. It Looks like my original code worked except for the buggy strict part. I’d tried total() but I must have stuffed that up during testing.
I have comment adding disabled, if I manually add plants there are no comment options on those but when save() used comments are allowed. I assume this is a PODS bug, is there any known workaround? I’ve googled for hours without luck.