skip to Main Content

I’m developing a blog SPA with WordPress headless CMS and Next.js.

WPGraphQL: I want to get json using GraphQL for posts in order of popularity with GraphQL API for WordPress.

The official action hook below states that it won’t work without the meta key, but I actually tried it, but it returned null.

I made a plugin and described the action hook as it is.

https://www.wpgraphql.com/docs/build-your-first-wpgraphql-extension/

https://www.wpgraphql.com/recipes/popular-posts/

2

Answers


  1. Chosen as BEST ANSWER

    Mutation is complete. By writing in functions.php on WordPress

        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 = GraphQLRelayRelay::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 GraphQLErrorUserError( __( '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
                ];
    
            }
        ] );
    } );
    

    However, I wrote a filter with the code below, but it didn't work with WP_Query. What's wrong with the one-piece filter code?

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

  2. The name of the filtered meta_key was incorrect.

    No Good:

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

    OK:

    $query_args['meta_key'] = 'views';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search