gatsbyjs/gatsby-source-wordpress-experimental

How to retrieve post metas from Geomashup ?

chawax opened this issue · 2 comments

Hi,

I use Geomashup extension in my Wordpress site and I am trying to get latitude and longitude that were set in some posts. They are copied to post metas as geo_latitude and geo_longitude but I can't find a way to retrieve them with WPGraphQL.

I thought this plugin could solve my problem : https://www.wpgraphql.com/extenstion-plugins/wpgraphql-meta/

But it doesn't.

Any idea how I could do ?

I could make it work using graphql_register_types action in my WordPress theme.
It registers a geo field in posts. But I can't find a way to filter on geo field in where clause.

add_action('graphql_register_types', function () {

    register_graphql_object_type('Geo', array(
        'description' => __('Geo Mashup coordinates associated with the object.', 'GeoMashup'),
        'fields' => array(
            'latitude' => array(
                'type' => float,
                'description' => __('The decimal latitude in the WGS 84 datum.', 'GeoMashup'),
            ),
            'longitude' => array(
                'type' => float,
                'description' => __('The decimal longitude in the WGS 84 datum.', 'GeoMashup'),
            ),
            'description' => array(
                'type' => string,
                'description' => __('An address or general description of the location.', 'GeoMashup'),
            ),
        ),
    ));

    register_graphql_field('Post', 'geo', array(
        'type' => 'Geo',
        'description' => __('Geo Mashup coordinates associated with the post', 'GeoMashup'),
        'resolve' => function ($post) {
            $location = GeoMashupDB::get_object_location('post', $post->ID);
            if (empty($location)) {
                return null;
            }
            return array(
                'latitude' => (float) $location->lat,
                'longitude' => (float) $location->lng,
                'description' => $location->address,
            );
        },
    ));

});

It can be used this way :

query MyQuery {
  posts {
    edges {
      node {
        title
        geo {
          latitude
          longitude
          description
        }
      }
    }
  }
}

Actually what I did was enough to solve my problem. My queries on geo field was runned on Gatsby data, so after the Wordpress source plugin did the job.