ID; if ($post instanceof Term) { $meta = YoastSEO()->meta->for_term($post->term_id); } else { // asPreview resolves to an autosave/revision, which Yoast stores no SEO data for. $post_id = wp_is_post_revision($post_id) ?: $post_id; if (get_option('show_on_front') === 'page' && (int) $post_id === (int) get_option('page_on_front')) { $meta = YoastSEO()->meta->for_home_page(); } else { $meta = YoastSEO()->meta->for_post($post_id); } } // https://developer.yoast.com/blog/yoast-seo-14-0-using-yoast-seo-surfaces/ $robots = $meta !== false ? $meta->robots : []; $indexable = $meta !== false ? $meta->indexable : null; // Get data $seo = [ 'title' => wp_gql_seo_format_string($meta !== false ? $meta->title : ''), 'metaDesc' => wp_gql_seo_format_string($meta !== false ? $meta->description : ''), 'focuskw' => function () use ($post) { return wp_gql_seo_format_string(get_post_meta($post->ID, '_yoast_wpseo_focuskw', true)); }, 'metaKeywords' => function () use ($post) { return wp_gql_seo_format_string(get_post_meta($post->ID, '_yoast_wpseo_metakeywords', true)); }, 'metaRobotsNoindex' => $robots['index'] ?? '', 'metaRobotsNofollow' => $robots['follow'] ?? '', 'robots' => wp_gql_seo_build_robots($robots, $indexable), 'opengraphTitle' => wp_gql_seo_format_string($meta !== false ? $meta->open_graph_title : ''), 'opengraphUrl' => wp_gql_seo_format_string($meta !== false ? $meta->open_graph_url : ''), 'opengraphSiteName' => wp_gql_seo_format_string($meta !== false ? $meta->open_graph_site_name : ''), 'opengraphType' => wp_gql_seo_format_string($meta !== false ? $meta->open_graph_type : ''), 'opengraphAuthor' => wp_gql_seo_format_string($meta !== false ? $meta->open_graph_article_author : ''), 'opengraphPublisher' => wp_gql_seo_format_string($meta !== false ? $meta->open_graph_article_publisher : ''), 'opengraphPublishedTime' => wp_gql_seo_format_string( $meta !== false ? $meta->open_graph_article_published_time : '' ), 'opengraphModifiedTime' => wp_gql_seo_format_string( $meta !== false ? $meta->open_graph_article_modified_time : '' ), 'opengraphDescription' => wp_gql_seo_format_string($meta !== false ? $meta->open_graph_description : ''), 'opengraphImage' => function () use ($context, $meta) { $id = wp_gql_seo_get_og_image($meta !== false ? $meta->open_graph_images : []); return $context->get_loader('post')->load_deferred(absint($id)); }, 'opengraphLocale' => wp_gql_seo_format_string($meta !== false ? $meta->open_graph_locale : ''), 'opengraphFbAppId' => wp_gql_seo_format_string($meta !== false ? $meta->open_graph_fb_app_id : ''), 'opengraphEnabled' => $meta !== false ? boolval($meta->open_graph_enabled) : null, 'twitterCardType' => wp_gql_seo_format_string($meta !== false ? $meta->twitter_card : ''), 'twitterTitle' => wp_gql_seo_format_string($meta !== false ? $meta->twitter_title : ''), 'twitterDescription' => wp_gql_seo_format_string($meta !== false ? $meta->twitter_description : ''), 'twitterImage' => function () use ($context, $meta) { $twitter_image = $meta !== false ? $meta->twitter_image : ''; if (empty($twitter_image)) { return null; } $id = wp_gql_seo_attachment_url_to_postid($twitter_image); return $context->get_loader('post')->load_deferred(absint($id)); }, 'twitterCreator' => wp_gql_seo_format_string($meta !== false ? $meta->twitter_creator : ''), 'twitterSite' => wp_gql_seo_format_string($meta !== false ? $meta->twitter_site : ''), 'canonical' => wp_gql_seo_format_string($meta !== false ? $meta->canonical : ''), 'readingTime' => floatval($meta !== false ? $meta->estimated_reading_time_minutes : ''), 'breadcrumbs' => function () use ($meta) { return $meta !== false ? $meta->breadcrumbs : []; }, 'cornerstone' => boolval($meta !== false && $meta->indexable ? $meta->indexable->is_cornerstone : false), 'fullHead' => function () use ($meta) { return wp_gql_seo_get_full_head($meta); }, 'head' => function () use ($meta) { return wp_gql_seo_get_head_obj($meta); }, 'relNext' => wp_gql_seo_format_string($meta !== false ? $meta->rel_next : ''), 'relPrev' => wp_gql_seo_format_string($meta !== false ? $meta->rel_prev : ''), 'breadcrumbTitle' => wp_gql_seo_format_string(wp_gql_seo_indexable_prop($indexable, 'breadcrumb_title')), 'objectPublishedAt' => wp_gql_seo_format_string(wp_gql_seo_indexable_prop($indexable, 'object_published_at')), 'objectLastModified' => wp_gql_seo_format_string(wp_gql_seo_indexable_prop($indexable, 'object_last_modified')), 'language' => wp_gql_seo_format_string(wp_gql_seo_indexable_prop($indexable, 'language')), 'region' => wp_gql_seo_format_string(wp_gql_seo_indexable_prop($indexable, 'region')), 'schema' => function () use ($meta) { return [ 'pageType' => $meta !== false && is_array($meta->schema_page_type) ? $meta->schema_page_type : [], 'articleType' => $meta !== false && is_array($meta->schema_article_type) ? $meta->schema_article_type : [], 'raw' => function () use ($meta) { return wp_json_encode($meta !== false ? $meta->schema : [], JSON_UNESCAPED_SLASHES); }, ]; }, 'analysis' => function () use ($indexable) { return wp_gql_seo_build_analysis($indexable); }, ]; return !empty($seo) ? $seo : null; } // Register GraphQL fields for NodeWithTitle and ContentNode types add_action('graphql_register_types', function () { register_graphql_field('ContentNode', 'seo', [ 'type' => 'PostTypeSEO', 'description' => __('The Yoast SEO data of the ContentNode', 'wp-graphql-yoast-seo'), 'resolve' => function ($post, array $args, AppContext $context) { return wp_gql_seo_get_post_type_graphql_fields($post, $args, $context); }, ]); register_graphql_field('NodeWithTitle', 'seo', [ 'type' => 'PostTypeSEO', 'description' => __('The Yoast SEO data of the ContentNode', 'wp-graphql-yoast-seo'), 'resolve' => function ($post, array $args, AppContext $context) { return wp_gql_seo_get_post_type_graphql_fields($post, $args, $context); }, ]); // If WooCommerce is active, add seo to product if (class_exists('WooCommerce')) { register_graphql_field('Product', 'seo', [ 'type' => 'PostTypeSEO', 'description' => __('The Yoast SEO data of the ContentNode', 'wp-graphql-yoast-seo'), 'resolve' => function ($post, array $args, AppContext $context) { return wp_gql_seo_get_post_type_graphql_fields($post, $args, $context); }, ]); } });