bonny / WordPress-Simple-Fields

WordPress plugin that extend Custom Fields to include textareas, WYSIWYG-editor, files, attachments, well basically anything
http://wordpress.org/extend/plugins/simple-fields/
88 stars 23 forks source link

Simple Fields with wp_insert_post() #86

Closed powerbuoy closed 11 years ago

powerbuoy commented 11 years ago

Perhaps this isn't an issue, but I'm experiencing some weird behaviour when adding posts programmatically and at the same time trying to attach simple fields to them.

I already have all the simple fields and post connectors setup in the admin and then in code, all I do is:

$newID = wp_insert_post(array('post_type' => 'items', 'post_title' => 'Test', 'post_content' => 'Testing'))

And then a few lines down:

simple_fields_set_value($newID, 'some_field', null, null, 'A value')

I'm not sure I've understood the fourth argument and have tried with both null and '__inherit__'.

First of all I always get this error:

Warning: Invalid argument supplied for foreach() in /home/powerbuoy/.../wp-content/plugins/simple-fields/functions.php on line 1439

Secondly, the added item doesn't have ANY of the fields I've set up in my post connector (and thus thirdly it doesn't have the field I set in code either).

Any idea what I'm doing wrong?

PS. I have another really weird problem which I'm not sure is related, but whenever I wp_insert_post() a post with another post_type than "post" it refuses to show up in the admin. The "number of posts"-count goes up and I can manually visit the edit-URL and from there edit the post but it refuses to show up in listings. I'm pretty deep into this project so it's hard to know what exactly might be wrong (I've attached an image of this very strange behaviour, as you can see there are >60 items but only 4 show up in the list(!)).

Thanks! missing-items

bonny commented 11 years ago

difficult to debug without having the code infront of me. some questions: do you have a post connector for that post type and is it set to be used by default? do you have the id of the post connector? can you try to pass that as the fourth parameter? what happens then?

your second problem seems like a problem with a cache or something. i don't thing simple fields has anything to do with that. try to do a http://codex.wordpress.org/Function_Reference/clean_post_cache or similar and see if that helps.

powerbuoy commented 11 years ago

I do have a post connector that is set as default for my custom post type.

However, if I pass in the post connector ID instead of null, it solves both of my problems! :)

It would be better if simple_fields_set_value() could figure out which connector to use though because I have a couple different ones that are used for different post types. I guess I can make that check myself and assign a different ID depending.

Anyway, this seems to work for now.

powerbuoy commented 11 years ago

Btw: I tried disabling ALL my plugins and run wp_insert_post() and a custom post type. It still wouldn't show up in the admin. I did not clear the DB though so the SimpleFields-stuff is still in there.

But like I said, when I passed in an ID instead of null the posts DO show up and they get their SimpleFields set. So it seems to have SOMETHING to do with SimpleFields... but I have no idea what it could be.

bonny commented 11 years ago

Ok, me neither at the moment. Will try to find time to test this and see if I can reproduce it.

powerbuoy commented 11 years ago

Another strange thing...

I'm actually working on migrating data from an old site (that didn't use SimpleFields) to a new one (that does), so I'm moving old Custom Fields to Simple Fields and that's why I use wp_insert_post() etc. One short example is:

<?php
# Get old price (if any)
$price = get_post_meta($row->ID, 'Price', true);

# If item had a price, set the simple field one
if ($price) {
    simple_fields_set_value($newID, 'price_from', null, 1, $price);
    simple_fields_set_value($newID, 'price_to', null, 1, $price);
}

The strange thing is that even though I have both price_from and price_to set as fields in the Simple Fields admin, ONLY "price_from" gets set during the migration. Do you see how this can be possible at all?? I don't.

I understand it must be really tricky to guess without seeing much code, but there really isn't much more than what I've shown here (just the same stuff over and over again for different custom fields) and "price_from" gets set properly but "price_to" doesn't (the old site only had "price_from" but I though I'd default "price_to" to the same value).

I'm severely tired so might be missing something obvious. I'll stop commenting here for a while now and try and get some sleep instead :)

powerbuoy commented 11 years ago

No there's definitely something strange going on. Changing the null to an ID worked for SOME posts, but not all. I have no idea what could be wrong but they simply refuse to show up in the admin (even though the actual post count increases). Never seen anything like it. I will investigate further though.

powerbuoy commented 11 years ago

Ok so I've solved both problems now. First of all I simply switched to using add_post_meta() instead of simple_fields_set_value() and grabbed the key names from the admin. Also I had to first add a _simple_fields_selected_connector custom field as well. One question here: How should I store the Simple Fields Map? Is it just serialize(array('lat' => '423', 'lon' => '342'))? (I haven't been able to verify whether this works).

The second problem with posts not showing up in the admin was entirely my fault. I've modified the WP search to allow visitors to sort by different fields and I forgot to make sure it wasn't run on admin pages.

Thanks for your help.

bonny commented 11 years ago

it's "lat" and "lng" (not "lon"), but that could work i think. i don't have the exact format in front of me right now, but it looks about right.