WPGraphQL:我想得到json使用GraphQL的帖子的流行度(视图计数)与GraphQL API for headless CMS WordPress使用MUTATION

2nc8po8w  于 2023-08-03  发布在  WordPress
关注(0)|答案(3)|浏览(80)

我正在开发一个博客SPA与WordPress headless CMS和Next.js。
WPGraphQL:我想让json使用GraphQL来发布帖子,按照GraphQL API for WordPress的受欢迎程度排序。
下面的官方动作钩子说明没有Meta键它就不能工作,但我实际上试过了,但它返回null。
我做了一个插件,并描述了动作挂钩。
https://www.wpgraphql.com/docs/build-your-first-wpgraphql-extension/
https://www.wpgraphql.com/recipes/popular-posts/

kyks70gy

kyks70gy1#

筛选的Meta_key的名称不正确。
不好:

$query_args['meta_key'] = 'wp_post_view_count';

字符串
好的:

$query_args['meta_key'] = 'views';

3ks5zfa0

3ks5zfa02#

变异完成。通过在WordPress上编写functions.php

add_action( 'graphql_register_types', function() {
    register_graphql_field( 'Post', 'viewCount', [
        'type' => 'Int',
        'description' => __( 'The number of views for the post', 'your-textdomain' ),
        'resolve' => function( $post ) {
            $views = get_post_meta( $post->ID, 'views', true );
            return isset( $views ) ? (int) $views : 0;
        }
    ] );

    register_graphql_mutation( 'viewPost', [
        'inputFields' => [
            'id' => [
                'type' => [
                    'non_null' => 'ID'
                ],
                'description' => __( 'ID of the post to increase view count', 'your-textdomain' ),
            ],
        ],
        'outputFields' => [
            'post' => [
                'type' => 'Post',
                'description' => __( 'The post that was viewed', 'your-textdomain' ),
            ],
        ],
        'mutateAndGetPayload' => function( $input ) {

            $id = null;

            if ( absint( $input['id'] ) ) {
                $id = absint( $input['id'] );
            } else {
                $id_parts = \GraphQLRelay\Relay::fromGlobalId( $input['id'] );
                if ( ! empty( $id_parts['id'] ) && absint( $id_parts['id'] ) ) {
                    $id = absint( $id_parts['id'] );
                }
            }

            
            if ( empty( $id ) || false == $post = get_post( absint( $id ) ) ) {
                throw new \GraphQL\Error\UserError( __( 'The ID entered is invalid' ) );
            }

            $current_views = (int) get_post_meta( $post->ID, 'views', true );
            update_post_meta( $post->ID, 'views', absint( $current_views + 1 ) );

            return [
                'post' => $post
            ];

        }
    ] );
} );

字符串
然而,我用下面的代码写了一个过滤器,但它不适用于WP_Query。一体式过滤器代码有什么问题?

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

    $values['VIEW_COUNT'] = [
        'value' => 'view_count',
        'description' => __( 'The number of views on the post', 'wp-graphql' ),
    ];

    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 ( ! isset( $orderby['field'] ) || 'view_count' !== $orderby['field'] ) {
                continue;
            }

            $query_args['meta_key'] = 'wp_post_view_count';
            $query_args['orderby'] = 'meta_value_num';
            $query_args['order'] = $orderby['order'];

        }

    }

    return $query_args;

}, 10, 3);

5n0oy7gb

5n0oy7gb3#

如何将viweCount设置为orderby?

add_filter( 'graphql_post_object_connection_orderby_enum_values', function( $values ) {
  $values['VIEW_COUNT'] = [
    'value'       => 'view_count',
    'description' => __( 'Order by the view count of the post', 'your-textdomain' ),
  ];
  return $values;
} );

字符串
这不是工作。

相关问题