Giter Club home page Giter Club logo

Comments (4)

lmartins avatar lmartins commented on June 26, 2024

Same here. Considering that it has been several months since this was opened, I wonder if this plugin is still the way to go to expose meta fields to GraphQL.

from wp-graphql-meta.

martin-braun avatar martin-braun commented on June 26, 2024

Same here. Considering that it has been several months since this was opened, I wonder if this plugin is still the way to go to expose meta fields to GraphQL.

@lmartins Unfortunately, it's not. Build your own meta fields using ACF and expose them using WPGraphQL for ACF.

from wp-graphql-meta.

lmartins avatar lmartins commented on June 26, 2024

Same here. Considering that it has been several months since this was opened, I wonder if this plugin is still the way to go to expose meta fields to GraphQL.

@lmartins Unfortunately, it's not. Build your own meta fields using ACF and expose them using WPGraphQL for ACF.

I've used that and works great. However in this case I was looking to pull meta fields created by WordPress internal APIs that do not go through ACF.

from wp-graphql-meta.

martin-braun avatar martin-braun commented on June 26, 2024

I've used that and works great. However in this case I was looking to pull meta fields created by WordPress internal APIs that do not go through ACF.

I just checked my code, because I had the same problem. I ended up registering the meta fields by myself like so:

add_action( 'graphql_register_types', function() {

	$metaFields = [
		'course' => [
			'announcement' => 'String',
			'duration_info' => 'String',
			'level' => 'String',
			'price' => 'String',
			'sale_price' => 'String',
			'sale_price_dates' => 'String',
			'sale_price_dates_end' => 'String',
			'sale_price_dates_start' => 'String',
			'skill_level' => 'String',
			'status' => 'String',
			'status_dates' => 'String',
			'status_dates_end' => 'String',
			'status_dates_start' => 'String',
		]
	];

	foreach ( $metaFields as $object_name => $fields ) {

		foreach ( $fields as $field_name => $field_type ) {
			
			$field_name_key = $field_name;
			$field_api_name = $object_name . '_' . $field_name;
			$field_api_name = str_replace( '_', '', ucwords( $field_api_name, '_' ) );
			register_graphql_field( $object_name, $field_api_name, [
					'type' => $field_type,
					'resolve' => function( $post ) use( $field_name_key ) {
						return get_post_meta( $post->ID, $field_name_key, true );
					}
			]);

		}

	}

} );

This is copied from my past project, it was exposing meta fields of MasterStudy, a theme / plugin collection for WordPress.
In the example the first level of $metaFields is the post_type, while the second level is the name of the post_meta field. The $field_api_name will be "CourseAnnouncement" in its first iteration.

You can see how the GraphQL fields are registered and a resolver is added that will just resolve the value by calling get_post_meta. This will not provide neat stuff like ordering and filtering, I think, it will just provide the fields in your model.

I have also code written to add ordering (orderby) and filtering (where) for ACF. It's not compatible with the sample above as it's for other fields, but maybe you can convert it so it adds the functionality to your fields, too:

// ORDERBY:

add_filter( 'graphql_PostObjectsConnectionOrderbyEnum_values', function( $values ) {

	$values['RELEVANCE'] = [
		'value' => 'relevance' // add custom meta fields as orderby options
	];

	return $values;

} );

add_filter( 'graphql_post_object_connection_query_args', function( $query_args, $source, $input ) {

	if ( isset( $input['where']['orderby'] ) && is_array( $input['where']['orderby'] ) ) {

		foreach( $input['where']['orderby'] as $orderby ) {

			if ( 
				$orderby['field'] === 'relevance' 
			) {
				$query_args['meta_key'] = $orderby['field'];
				$query_args['orderby'] = 'meta_value_num';
				$query_args['order'] = $orderby['order'];
			}

		}

	}

	return $query_args;

}, 10, 3);

// WHERE:

add_action( 'graphql_register_types', function () {

	$root_query_type = 'RootQuery';
	$course_category_type = 'CourseCategory';
	$course_type = "Course";

	register_graphql_field( $course_category_type . 'To' . $course_type . 'ConnectionWhereArgs', 'relevance_gte', [
		'type' => 'Number',
	] );

	register_graphql_field( $root_query_type . 'To' . $course_type . 'ConnectionWhereArgs', 'relevance_gte', [
		'type' => 'Number',
	] );

	register_graphql_field( $root_query_type . 'To' . $course_type . 'ConnectionWhereArgs', 'tags_like', [
		'type' => 'String',
	] );
	
} );

add_filter( 'graphql_post_object_connection_query_args', function ( $query_args, $source, $args, $context, $info ) {

	$meta_query = [];

	if ( isset( $args['where']['relevance_gte'] ) ) {

		$meta_query['tags'][] = [
			'key' => 'relevance',
			'value' => $args['where']['relevance_gte'],
			'compare' => '>='
		];

	}

	if ( isset( $args['where']['tags_like'] ) ) {
		$tags_filter_arr = explode( ';', $args['where']['tags_like'] );

		$meta_query['tags'] = [
			'relation' => 'OR'
		];
		foreach ( $tags_filter_arr as $tags_filter_arr_item ) {
			if($tags_filter_arr_item) {
				$meta_query['tags'][] = [
					'key' => 'tags',
					'value' => str_replace( ' ', '', $tags_filter_arr_item ) . ';',
					'compare' => 'LIKE'
				];
			}
		}
	}

	$query_args['meta_query'] = $meta_query;
	return $query_args;
}, 10, 5 );

You can see how it's programmed only for two custom field called "relevance" and "tags" and a specific operation "gte" (greater than equals) for the "relevance" as well as a like comparer for the "tags" field. It might give you help to understand in what direction you have to move to get this stuff for your custom fields. You would program a lot of stuff to give your fields everything that is possible, so think about what meta operations you really need.

If you don't need order or filter operations on the fields, consider yourself lucky, because you won't need to progress the latter example.

Good luck!

from wp-graphql-meta.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.