• This repository has been archived on 23/Jul/2024
  • Stars
    star
    626
  • Rank 71,755 (Top 2 %)
  • Language
    PHP
  • Created over 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

WPGraphQL for Advanced Custom Fields

WPGraphQL for Advanced Custom Fields

WPGraphQL for Advanced Custom Fields automatically exposes your ACF fields to the WPGraphQL Schema.

Install and Activate

WPGraphQL for Advanced Custom Fields is not currently available on the WordPress.org repository, so you must download it from Github, or Composer.

Installing From Github

To install the plugin from Github, you can download the latest release zip file, upload the Zip file to your WordPress install, and activate the plugin.

Click here to learn more about installing WordPress plugins from a Zip file.

Installing from Composer

composer require wp-graphql/wp-graphql-acf

Dependencies

In order to use WPGraphQL for Advanced Custom Fields, you must have WPGraphQL and Advanced Custom Fields (free or pro) installed and activated.

Adding Fields to the WPGraphQL Schema

TL;DR: Here's a video showing an overview of usage.

Advanced Custom Fields, or ACF for short, enables users to add Field Groups, either using a Graphical User Interface, PHP code, or local JSON to various screens in the WordPress dashboard, such as (but not limited to) the Edit Post, Edit User and Edit Term screens.

Whatever method you use to register ACF fields to your WordPress site should work with WPGraphQL for Advanced Custom Fields. For the sake of simplicity, the documentation below will primarily use the Graphic User Interface for examples.

Add ACF Fields to the WPGraphQL Schema

The first step in using Advanced Custom Fields with WPGraphQL is to create an ACF Field Group.

By default, field groups are not exposed to WPGraphQL. You must opt-in to expose your ACF Field Groups and fields to the WPGraphQL Schema as some information managed by your ACF fields may not be intended for exposure in a queryable API like WPGraphQL.

Show in GraphQL Setting

To have your ACF Field Group show in the WPGraphQL Schema, you need to configure the Field Group to "Show in GraphQL".

Using the ACF GUI

When using the ACF Graphic User Interface for creating fields, WPGraphQL for Advanced Custom Fields adds a Show in GraphQL field to Field Groups.

Setting the value of this field to "Yes" will show the field group in the WPGraphQL Schema, if a GraphQL Field Name is also set

Show in GraphQL Setting for ACF Field Groups

Registering Fields in PHP

When registering ACF Fields in PHP, you need to add show_in_graphql and graphql_field_name when defining your field group. See below as an example.

function my_acf_add_local_field_groups() {

	acf_add_local_field_group(array(
		'key' => 'group_1',
        'title' => 'My Group',
        'show_in_graphql' => true,
        'graphql_field_name' => 'myGroup',
		'fields' => array (
			array (
				'key' => 'field_1',
				'label' => 'Sub Title',
				'name' => 'sub_title',
				'type' => 'text',
			)
		),
		'location' => array (
			array (
				array (
					'param' => 'post_type',
					'operator' => '==',
					'value' => 'post',
				),
			),
		),
	));

}

add_action('acf/init', 'my_acf_add_local_field_groups');

Each individual field will inherit its GraphQL name from the supplied name tag. In this example, sub_title will become subTitle when requested through GraphQL. If you want more granular control, you can pass graphql_field_name to each individual field as well.

Supported Fields

In order to document interacting with the fields in GraphQL, an example field group has been created with one field of each type.

To replicate the same field group documented here you can download the example field group and import it into your environment.

For the sake of documentation, this example field group has the location rule set to "Post Type is equal to Post", which will allow for the fields to be entered when creating and editing Posts in the WordPress dashboard, and will expose the fields to the Post type in the WPGraphQL Schema.

Location rule set to Post Type is equal to Post

Options Pages

Reference: https://www.advancedcustomfields.com/add-ons/options-page/

To add an option page and expose it to the graphql schema, simply add 'show_in_graphql' => true when you register an option page.

Example Usage

function register_acf_options_pages() {

    // check function exists
    if ( ! function_exists( 'acf_add_options_page' ) ) {
        return;
    }

    // register options page
    $my_options_page = acf_add_options_page(
        array(
            'page_title'      => __( 'My Options Page' ),
            'menu_title'      => __( 'My Options Page' ),
            'menu_slug'       => 'my-options-page',
            'capability'      => 'edit_posts',
            'show_in_graphql' => true,
        )
    );
}

add_action( 'acf/init', 'register_acf_options_pages' )
Example Query
query GetMyOptionsPage {
    myOptionsPage {
        someCustomField
    }
}

Alternatively, it's you can check the Fields API Reference to learn about exposing your custom fields to the Schema.

Location Rules

Advanced Custom Fields field groups are added to the WordPress dashboard by being assigned "Location Rules".

WPGraphQL for Advanced Custom Fields uses the Location Rules to determine where in the GraphQL Schema the field groups/fields should be added to the Schema.

For example, if a Field Group were assigned to "Post Type is equal to Post", then the field group would show in the WPGraphQL Schema on the Post type, allowing you to query for ACF fields of the Post, anywhere you can interact with the Post type in the Schema.

Supported Locations

@todo: Document supported location rules and how they map from ACF to the WPGraphQL Schema

Why aren't all location rules supported?

You might notice that some location rules don't add fields to the Schema. This is because some location rules are based on context that doesn't exist when the GraphQL Schema is generated.

For example, if you have a location rule to show a field group only on a specific page, how would that be exposed the the Schema? There's no Type in the Schema for just one specific page.

If you're not seeing a field group in the Schema, look at the location rules, and think about how the field group would be added to a Schema that isn't aware of context like which admin page you're on, what category a Post is assigned to, etc.

If you have ideas on how these specific contextual rules should be handled in WPGraphQL, submit an issue so we can consider how to best support it!

More Repositories

1

wp-graphql

🚀 GraphQL API for WordPress
PHP
3,650
star
2

wp-graphql-woocommerce

Add WooCommerce support and functionality to your WPGraphQL server
PHP
644
star
3

wp-graphql-jwt-authentication

Authentication for WPGraphQL using JWT (JSON Web Tokens)
PHP
337
star
4

wp-graphiql

GraphiQL IDE, fine tuned for use with WPGraphQL
JavaScript
289
star
5

gatsby-wpgraphql-blog-example

Demo showing how to use WPGraphQL as the source for Gatsby Sites
JavaScript
147
star
6

wp-graphql-custom-post-type-ui

Adds Settings to the Custom Post Type UI plugin to show Post Types in WPGraphQL
PHP
82
star
7

wpgraphql-acf

Re-architecture of WPGraphQL for ACF
PHP
79
star
8

wp-graphql-smart-cache

Smart Caching & Cache Invalidation for WPGraphQL
PHP
60
star
9

wp-graphql-meta-query

WPGraphQL Extension: Adds "meta_query" support to postObject connection queries using WP_Query
PHP
51
star
10

wp-graphql-tax-query

Adds `tax_query` support to postObject connection queries using WP_Query
PHP
46
star
11

wpgraphql.com

The main website for WPGraphQL.com. This is a NextJS site pulling data from WordPress + WPGraphQL.
JavaScript
36
star
12

wp-graphql-block-editor

EXPERIMENTAL plugin extending WPGraphQL to support querying (Gutenberg) Blocks as data, using Server Side Block registries to map Blocks to the GraphQL Schema.
PHP
32
star
13

wordflix-single-page-app

This is a Single Page App that was created for use in a workshop showing how to build Single Page Apps using WordPress, React and GraphQL.
HTML
26
star
14

docs.wpgraphql.com

DEPRECATED. The docs now exist within the WPGraphQL repo:
JavaScript
21
star
15

graphql-gutenblock-example

Example Gutenberg Block using WPGraphQL to populate the data
JavaScript
18
star
16

wpgraphql-gatsby-kanban

Kanban board created with WPGraphQL and Gatsby, for demo at WordCamp US
JavaScript
17
star
17

woographql-subscriptions

Adds WooCommerce Subscriptions types and functionality to your WPGraphQL API.
PHP
14
star
18

wp-graphql-insights

Insights and Logging for WPGraphQL
PHP
12
star
19

examples

JavaScript
11
star
20

wp-graphql-dad-jokes

Get a random Dad Joke returned via GraphQL query using the WPGraphQL WordPress plugin (https://github.com/wp-graphql/wp-graphql)
Shell
11
star
21

wpgraphql-ide

A next-gen query editor for WPGraphQL 🚀
JavaScript
10
star
22

playground.wpgraphql.com

GraphiQL playground, specifically for WPGraphQL and it's extensions
JavaScript
10
star
23

wp-graphql-subscriptions

Subscriptions for WPGraphQL
10
star
24

wp-graphiql-2

Temporary home of v2.0 of WPGraphiQL, the GraphiQL IDE that ships with WPGraphQL
CSS
8
star
25

wp-graphql-testcase

WPGraphQL API Unit Testing Library supports WP-PHPUnit(PHPUnit) and WPBrowser(Codeception)
PHP
7
star
26

wp-graphql-notifications-connection-example

This is an example plugin showing how to register custom connections to WPGraphQL, pulling data from custom SQL tables.
PHP
7
star
27

wp-graphql-fieldmanager

GraphQL bindings for WordPress Fieldmanager, by Alley Interactive
PHP
6
star
28

wp-graphql-react-native-example

This is a repository showing a basic example of using React Native to interact with WPGraphQL. NOTE: This is an example project and should not necessarily be considered a guide for best practices.
Java
5
star
29

wp-graphql-federation

Extension to WPGraphQL, providing support for Apollo Federation
PHP
4
star
30

wp-graphql-theme

WordPress Theme for wpgraphql.com
CSS
4
star
31

wp-settings-api

API for building setting pages in WordPress.
3
star
32

wp-graphql-blue-guitar

Example plugin showing how to register a GraphQL field to the WPGraphQL Schema
PHP
3
star
33

wp-graphql-api-docs

WPGraphQL API Docs generated using ApiGen
HTML
2
star
34

wp-graphql-e2e-tests-example

An example plugin showing how to add End to End tests to a WordPress plugin
JavaScript
2
star
35

wp-graphql-docs

Documentation for your WP GraphQL Schema
HTML
2
star
36

acf.wpgraphql.com

JavaScript
1
star
37

dashboard.wpgraphql.com

Prototype of a WordPress Dashboard built on Gatsby, using WPGraphQL and Ant Design
1
star
38

wp-graphiql-desktop

GraphiQL, tailored specifically for working with WPGraphQL as a Desktop App.
JavaScript
1
star
39

wp-graphql-co-authors-plus

Adds Co Authors Plus support to WPGraphQL
1
star