Title: Programatically inserting posts doesn&#8217;t generate lat/lng data
Last modified: October 15, 2020

---

# Programatically inserting posts doesn’t generate lat/lng data

 *  [eclev91](https://wordpress.org/support/users/eclev91/)
 * (@eclev91)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/programatically-inserting-posts-doesnt-generate-lat-lng-data/)
 * I’ve got a WP-CLI command set up that syncs store locator locations from a third
   party. It boils down to running:
 * `wp_update_post($args);`
 * This inserts and updates locations just fine. However, your `save_post` hook,
   which is responsible for a few things, including geocoding the address, short-
   circuits if it doesn’t see the anticipated fields in the `$_POST` array. Obviously,
   when inserting posts programmatically, there isn’t anything in the `$_POST` array.
   It also short-circuits on a `current_user_can` call, and once again, during WP-
   CLI operations, there is no user.
 * It’d be great if this were compatible with all situations in which the `save_post`
   action fires. Pretty much everything here either returns false or is depending
   on data that may not be present:
 *     ```
       if ( empty( $_POST['wpsl_meta_nonce'] ) || !wp_verify_nonce( $_POST['wpsl_meta_nonce'], 'save_store_meta' ) )
           return;
   
       if ( !isset( $_POST['post_type'] ) || 'wpsl_stores' !== $_POST['post_type'] )
           return;
   
       // ...
   
       if ( !current_user_can( 'edit_post', $post_id ) )
           return;
   
       $this->store_data = $_POST['wpsl'];
       ```
   
 * The `post_type` check could be resolved by changing the hook you subscribe to
   to `save_post_wpsl_stores` (see [here](https://developer.wordpress.org/reference/hooks/save_post_post-post_type/)).
 * The capability check can _probably_ be safely omitted.
 * And then `$this->store_data` would have to depend on post meta fetched from the
   database rather than the `$_POST` information.
 * The rest I would have to leave you to decide.
 * Let me know if I’m totally misunderstanding why geocoding isn’t happening on 
   our import, or if you have a clever way of getting around this in the meantime.
    -  This topic was modified 5 years, 5 months ago by [eclev91](https://wordpress.org/support/users/eclev91/).
    -  This topic was modified 5 years, 5 months ago by [eclev91](https://wordpress.org/support/users/eclev91/).
    -  This topic was modified 5 years, 5 months ago by [eclev91](https://wordpress.org/support/users/eclev91/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [eclev91](https://wordpress.org/support/users/eclev91/)
 * (@eclev91)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/programatically-inserting-posts-doesnt-generate-lat-lng-data/#post-13540525)
 * (and if you have this thing in git somewhere, I’d be happy to work on a pull 
   request to resolve some of these items)
 *  Plugin Author [Tijmen Smit](https://wordpress.org/support/users/tijmensmit/)
 * (@tijmensmit)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/programatically-inserting-posts-doesnt-generate-lat-lng-data/#post-13555621)
 * Sorry for the late response, and thanks for pointing this out. I wasn’t aware
   of this issue. I have to look into the best way to make this work with WP-CLI.
 * I do plan to implement support for the REST API and allow insert / updates in
   combination with [application passwords](https://wptavern.com/wordpress-5-6-to-introduce-application-passwords-for-rest-api-authentication)
   in the 3.0 update.
 * I never really used the WP CLI myself, but I assume you can make calls to the
   REST API from it?
 * As a workaround for now you could call [this function](https://plugins.trac.wordpress.org/browser/wp-store-locator/tags/2.2.233/inc/wpsl-functions.php#L480)
   with the used address and then [update](https://developer.wordpress.org/reference/functions/update_post_meta/)
   the corresponding meta data yourself.
    -  This reply was modified 5 years, 5 months ago by [Tijmen Smit](https://wordpress.org/support/users/tijmensmit/).

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Programatically inserting posts doesn’t generate lat/lng data’ is closed
to new replies.

 * ![](https://ps.w.org/wp-store-locator/assets/icon-256x256.jpg?rev=1007784)
 * [WP Store Locator](https://wordpress.org/plugins/wp-store-locator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-store-locator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-store-locator/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-store-locator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-store-locator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-store-locator/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Tijmen Smit](https://wordpress.org/support/users/tijmensmit/)
 * Last activity: [5 years, 5 months ago](https://wordpress.org/support/topic/programatically-inserting-posts-doesnt-generate-lat-lng-data/#post-13555621)
 * Status: not resolved