Skip to content

Query post view #1860

Answered by jasonbahl
IRediTOTO asked this question in Q&A
Apr 16, 2021 · 1 comments · 7 replies
Discussion options

You must be logged in to vote

@IRediTOTO I know I'm a bit late to responding here, but if you store the "view count" as post meta as described in the article, you should be able to register a field to the GraphQL Schema that can resolve that value.

If you want the field to exist only on one post type, such as the "post" post type, you could register the field like so:

add_action( 'graphql_register_types', function() {

  register_graphql_field( 'Post', 'viewCount', [ 
    'type' => 'Int',
    'description' => __( 'The number of times the post was viewed', 'your-textdomain' ),
    'resolve' => function( $post, $args, $context, $info ) {
      // $post is an instance of a WPGraphQL Post model, 
      // so we can get th…

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@metawise
Comment options

@masitings
Comment options

@AliNaraghiA
Comment options

@dotwise
Comment options

@masitings
Comment options

Answer selected by jasonbahl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
7 participants