Programatically inserting posts doesn’t 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_posthook, which is responsible for a few things, including geocoding the address, short-circuits if it doesn’t see the anticipated fields in the$_POSTarray. Obviously, when inserting posts programmatically, there isn’t anything in the$_POSTarray. It also short-circuits on acurrent_user_cancall, 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_postaction 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_typecheck could be resolved by changing the hook you subscribe to tosave_post_wpsl_stores(see here).The capability check can probably be safely omitted.
And then
$this->store_datawould have to depend on post meta fetched from the database rather than the$_POSTinformation.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.
The topic ‘Programatically inserting posts doesn’t generate lat/lng data’ is closed to new replies.