Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to set latitude and longitude from wordpress API rest ? #21

Closed
charmosz opened this issue Jul 7, 2021 · 2 comments
Closed

Comments

@charmosz
Copy link

charmosz commented Jul 7, 2021

I use integromat and wordpress rest api to create a post with custom fields (made with ACF). I would like to set latitude and longitude of this post directly through the API instead of doing it manually in wordpress after the creation. Is it possible ?
By the way thank you for this great plugin !

@charmosz charmosz changed the title Possible to populate latitude and longitude from wordpress API rest Is it possible to set latitude and longitude from wordpress API rest ? Jul 7, 2021
@Socrapop
Copy link
Owner

Socrapop commented Jul 7, 2021

Dear @charmosz,

I don't know the limit of the Wordpress Rest API, but here is what you need:
The latitude/longitude are saved inside a unique metadata : _latlngmarker.
This metadata is a json array :

// latitude and longitude : string "12.34578"
// markerdata = 'default' or array 
//    $markerdata[0] : Image URL
//    $markerdata[1] : Image Width
//    $markerdata[2] : Image Height
// multiplemarkers = false (V2 will introduce multiplemarker)
// customtitle, customexcerpt: string
// customthumbnail: the media ID as a string.
$latlngmarker = json_encode(compact('latitude', 'longitude', 'markerdata', 'multiplemarkers', 'customtitle', 'customexcerpt', 'customthumbnail'), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_APOS);

Here is what _latlngmarker look like in the database:
{"latitude":"43.97806","longitude":"0.39126","markerdata":["https://yoursite.url/wp-content/uploads/2020/11/cttm_markers-red.png",32,45],"multiplemarkers":false,"customtitle":"","customexcerpt":"","customthumbnail":"80"}

Then, I use those functions, the first is to set the marker data define above, the second adds a private taxonomy to the post, so the plugin can query it a lot faster than with a metadata query. This private taxonomy is mandatory for the query to work:

 // Update post meta 
 update_post_meta($post_id, '_latlngmarker', $latlngmarker);

// add terms in private taxonomy
 wp_set_post_terms($post_id, 'hasmarker', 'cttm-markers-tax', false);

I'm not sure if the REST API allows you to set private taxonomies, I never used it !

@charmosz
Copy link
Author

charmosz commented Jul 8, 2021

Wonderful !!! Thanks.

FYI, I didn't succeed in setting the taxonomy through the API. As a workaround I set default_term in the register_taxonomy function. So now every new custom post has this term by default.
register_taxonomy( 'cttm-markers-tax', $public_posttypes, array( 'label' => __('Travelers Map Markers'), 'public' => false, 'rewrite' => false, 'default_term' => 'hasmarker' ) );

@charmosz charmosz closed this as completed Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants