gpt4 book ai didi

wordpress - 如何在 graphql-wp 高级自定义字段上添加自定义过滤器

转载 作者:行者123 更新时间:2023-12-04 15:09:25 25 4
gpt4 key购买 nike

我正在尝试为使用高级自定义字段插件定义的自定义字段添加新过滤器。
enter image description here
我想过滤艺术家的年龄,我查阅了一些文档,但对进展感到困惑。 (我是wordpress新手)
我已将以下代码行添加到我的functions.php 中,遗憾的是没有任何显着的结果。

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);

我希望实现的是在由高级自定义字段定义的艺术家元数据字段组中的年龄字段上过滤艺术家,下图进行说明。
enter image description here

最佳答案

感谢@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);

关于wordpress - 如何在 graphql-wp 高级自定义字段上添加自定义过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65461514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com