4

I am trying to add a new filter for a custom field defined with the Advanced Custom Fields plugin.

enter image description here

I want to filter on the age of artists, I looked up some documentation but got confused in the progress. (I am a wordpress newbie)

I have added the following lines of code to my functions.php, sadly without any noticable results.

add_action('graphql_register_types', function () {

    $customposttype_graphql_single_name = "artist";

    register_graphql_field('RootQueryTo' . $customposttype_graphql_single_name . 'ConnectionWhereArgs', 'age', [
        'type' => 'ID',
        'description' => __('The ID of the post object to filter by', 'your-textdomain'),
    ]);
});

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

    $post_object_id = $args['where']['age'];

    if (isset($post_object_id)) {
        $query_args['meta_query'] = [
            [
                'key' => 'artist_metadata',
                'value' => $post_object_id,
                'compare' => '='
            ]
        ];
    }

    return $query_args;
}, 10, 5);

What I wish to achieve is to filter artists on the age field which is in the artist_metadata field group defined by Advanced Custom Fields, image below for clarification.

enter image description here

TheAlphaKarp
  • 159
  • 2
  • 13

1 Answers1

4

Managed to make it work thanks to @xadm

add_action('graphql_register_types', function () {

    // PascalCase here
    $customposttype_graphql_single_name = "Artist";

    register_graphql_field('RootQueryTo' . $customposttype_graphql_single_name . 'ConnectionWhereArgs', 'age', [
        // Integer because age
        'type' => 'Integer',
        'description' => __('The ID of the post object to filter by', 'your-textdomain'),
    ]);
});

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

    $post_object_id = $args['where']['age'];

    if (isset($post_object_id)) {
        $query_args['meta_query'] = [
            [
                // The key should be age, not artist_metadata
                'key' => 'age',
                'value' => $post_object_id,
                'compare' => '='
            ]
        ];
    }

    return $query_args;
}, 10, 5);

TheAlphaKarp
  • 159
  • 2
  • 13