質問編集履歴
1
文法の修正・追加
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -15,9 +15,7 @@ 
     | 
|
| 
       15 
15 
     | 
    
         | 
| 
       16 
16 
     | 
    
         
             
            ```function.php
         
     | 
| 
       17 
17 
     | 
    
         
             
            add_action('graphql_register_types', function () {
         
     | 
| 
       18 
     | 
    
         
            -
            	// 投稿タイプのリストを取得
         
     | 
| 
       19 
     | 
    
         
            -
            	$post_types = ['Post', 'works_post']; 
     | 
| 
      
 18 
     | 
    
         
            +
            	$post_types = ['Post', 'works_post'];
         
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
19 
     | 
    
         
             
            	foreach ($post_types as $post_type) {
         
     | 
| 
       22 
20 
     | 
    
         
             
            			register_graphql_field($post_type, 'nextPost', [
         
     | 
| 
       23 
21 
     | 
    
         
             
            					'type' => 'Post',
         
     | 
| 
         @@ -70,3 +68,36 @@ 
     | 
|
| 
       70 
68 
     | 
    
         
             
            ### 試したこと
         
     | 
| 
       71 
69 
     | 
    
         | 
| 
       72 
70 
     | 
    
         
             
            chatGPTで回答されたコードは一通り試しました。
         
     | 
| 
      
 71 
     | 
    
         
            +
            その中で、以下のコードは希望通りWPGraphQLに「worksACF > worksSubtitle」が表示されたのですが、**"previousPost": null** のエラーが出て、一つ前の投稿の取得自体がエラーが出ました。
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
            ```function.php
         
     | 
| 
      
 74 
     | 
    
         
            +
            add_action('graphql_register_types', function () {
         
     | 
| 
      
 75 
     | 
    
         
            +
            	$post_types = ['Post', 'works_post'];
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
            	foreach ($post_types as $post_type) {
         
     | 
| 
      
 78 
     | 
    
         
            +
            			register_graphql_field($post_type, 'nextPost', [
         
     | 
| 
      
 79 
     | 
    
         
            +
            					'type' => $post_type,
         
     | 
| 
      
 80 
     | 
    
         
            +
            					'description' => '次の投稿を取得します。',
         
     | 
| 
      
 81 
     | 
    
         
            +
            					'resolve' => function($root, $args, $context, $info) use ($post_type) {
         
     | 
| 
      
 82 
     | 
    
         
            +
            							$next_post = get_adjacent_post(false, '', false, $post_type);
         
     | 
| 
      
 83 
     | 
    
         
            +
            							if ($next_post) {
         
     | 
| 
      
 84 
     | 
    
         
            +
            									return \WPGraphQL\Data\DataSource::resolve_post_object($next_post->ID, $context);
         
     | 
| 
      
 85 
     | 
    
         
            +
            							}
         
     | 
| 
      
 86 
     | 
    
         
            +
            							return null;
         
     | 
| 
      
 87 
     | 
    
         
            +
            					}
         
     | 
| 
      
 88 
     | 
    
         
            +
            			]);
         
     | 
| 
      
 89 
     | 
    
         
            +
             
     | 
| 
      
 90 
     | 
    
         
            +
            			register_graphql_field($post_type, 'previousPost', [
         
     | 
| 
      
 91 
     | 
    
         
            +
            					'type' => $post_type,
         
     | 
| 
      
 92 
     | 
    
         
            +
            					'description' => '前の投稿を取得します。',
         
     | 
| 
      
 93 
     | 
    
         
            +
            					'resolve' => function($root, $args, $context, $info) use ($post_type) {
         
     | 
| 
      
 94 
     | 
    
         
            +
            							$prev_post = get_adjacent_post(false, '', true, $post_type);
         
     | 
| 
      
 95 
     | 
    
         
            +
            							if ($prev_post) {
         
     | 
| 
      
 96 
     | 
    
         
            +
            									return \WPGraphQL\Data\DataSource::resolve_post_object($prev_post->ID, $context);
         
     | 
| 
      
 97 
     | 
    
         
            +
            							}
         
     | 
| 
      
 98 
     | 
    
         
            +
            							return null;
         
     | 
| 
      
 99 
     | 
    
         
            +
            					}
         
     | 
| 
      
 100 
     | 
    
         
            +
            			]);
         
     | 
| 
      
 101 
     | 
    
         
            +
            	}
         
     | 
| 
      
 102 
     | 
    
         
            +
            });
         
     | 
| 
      
 103 
     | 
    
         
            +
            ```
         
     |