Insert/Update Listings from Plugin
This topic contains 17 replies, has 3 voices, and was last updated by Alex Rollin 7 years, 3 months ago.
We have moved to a support ticketing system and our forums are now closed.
Open Support Ticket-
AuthorPosts
-
November 28, 2017 at 1:44 pm #407149
Hello,
We have bought and setup GeoDirectory core plugin and the add-on for Custom Post Types. We use a cpt for all of our directory listings.
We are writing a plugin that connects to our intranet API and will import data from our intranet into the geodirectory. Can you please point me in the correct direction to be able to:
– insert new listings with relevant data for fields and categories
– update existing listings based on a custom field valueI checked the API docs, and the codex but couldn’t find anything.
I also checked the add-listing template but that only uses actions and I need a way to call a function that will handle the database interaction naturally so to not cause future issues with updates..Many thanks in advance
November 28, 2017 at 3:07 pm #407179Hi Robert,
I am actually in the middle of rewriting this part, in future it will hook into the standard wp_insert_post() and wp_update_post() functions, you will simply pass the extra fields into the post data.
If you need to implement this sooner this is the current function:
https://github.com/GeoDirectory/geodirectory/blob/d1298ab275a7e54b36bb1cd33abacda42df66df2/geodirectory-functions/post_functions.php#L133Thanks,
Stiofan
November 28, 2017 at 4:39 pm #407192Thank you Stiofan for that link! Very helpful
To confirm, geodir_save_listing() can be used for both insert and update right? Would you be able to give a simple example of an update based on custom field?Which function would I use to get a listings data? Maybe something like geodir_get_listing?
Thanks again!
November 28, 2017 at 6:19 pm #407206My use case is:
We have a variable called$testvalue = "testval";
We want to use this $testvalue to update geo directory listings that have a custom field “Test Val” as the same value a this variable $testvalue
Also, we want to be updating other custom fields within the geodir_save_listing() callThe goal is to insert a record if it does not already exist with the custom field as $testvalue and if it exists, then update instead
November 28, 2017 at 6:45 pm #407212Yes it is used for insert and update. you can look at out dummy data as an example:
https://github.com/GeoDirectory/geodirectory/blob/master/geodirectory-admin/dummy-data/standard_places.phpYou can get all post meta info with https://wpgeodirectory.com/codex/codex/geodirectory_functions/geodir_get_post_info/
For individual fields you can use the GD functions that work the same as the WP ones:
geodir_save_post_meta
geodir_get_post_meta
geodir_delete_post_metaStiofan
November 28, 2017 at 6:52 pm #407221Thank you very much.
Lastly, for custom fields, is there some prefix?
If i made a custom field called “Test Val” and it’s html variable is test_val then what would the meta key be? Would it be geodir_test_val or would it be test_val?
November 28, 2017 at 7:40 pm #407231Hello!
The prefix is “geodir_”, so, “geodir_test_val”
thanks
November 28, 2017 at 7:45 pm #407233Thanks Alex.
I tried this simple query to retrieve listings by custom field value. Since I first need the $post_id to use geodir_get_post_info() or _get_post_meta() —
$existing = (new WP_Query(array( "meta_query" => array( array( "key" => "geodir_test_val", "value" => "100", // 'compare' => 'IN' ) ) )))->posts;
However this returns an empty array, however there is a listing with the custom field value “100” as I created it manually to test with.
Much thanks for info so far. It has been helpful and insightful.
November 28, 2017 at 8:22 pm #407250Hello!
We will be able to announce a release date for V2 in the near future. As Stiofan mentioned, the new features are currently WIP.
https://wpgeodirectory.com/support/topic/insertupdate-listings-from-plugin/#post-407179
November 28, 2017 at 8:25 pm #407252This is for simply getting the post data nbased on a meta key. That is not even possible?
I know update/insert what stiofan mentioned.. that’s already covered. But i’m talking about just getting the post data ie post id.
November 28, 2017 at 8:29 pm #407254Hello!
We don’t support programmatic use of the plugin, actually.
We do support the REST API, if we could persuade you to consider that route.
I see you are a very talented developer. I will flag this, but, please be patient with us because this is not covered within our support policy.
Thanks for your patience.
November 28, 2017 at 10:18 pm #407275What would the rest api URL be for filtering the listing by custom field?
November 28, 2017 at 10:41 pm #407285Hello!
It’s an Addon:
https://github.com/GeoDirectory/geodir_apiHere is a docs site, also, for endpoints:
https://wpgeo.directory/rest-api/geodirectory-rest-api-endpoints/November 28, 2017 at 11:10 pm #407296Ok so I installed that and looked at the documentation. Straight forward but I see no examples or references on filtering.
I tried normal wordpress filtering but it didn’t work
$curl = curl_init(); $field_string = http_build_query(array( 'filter[meta_query]' => array( array ( 'key' => 'geodir_test_val', 'value' => "1000", ) ), )); curl_setopt_array($curl, array ( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => site_url('wp-json/geodir/v1/customdir?' . $field_string) ) ); $existing = json_decode(curl_exec($curl));
However the above code brings back all listings and not filtered by field/meta.
I understand replies take a little bit of time. Are you able to guide me here as I don’t see anything further on the REST API docs.. thanks!
November 29, 2017 at 11:19 am #407360For filtering you would need to use a custom query at the moment, and join the post table with the place_details table instead of the post_meta table.
something like:
` global $wpdb, $table_prefix, $plugin_prefix,$post;
$post_id = $post->ID;
$post_type = $post->post_type;
$table = $plugin_prefix . $post_type . ‘_detail’;$post_arr = $wpdb->get_results($wpdb->prepare(
“SELECT * FROM $wpdb->posts p JOIN ” . $table . ” gd ON gd.post_id=p.ID WHERE p.ID=%d LIMIT 1″,
array($post_id)
));`Stiofan
-
AuthorPosts
We have moved to a support ticketing system and our forums are now closed.
Open Support Ticket