質問編集履歴

1

現在の状況を具体的にした

2018/04/20 22:02

投稿

deep
deep

スコア20

test CHANGED
File without changes
test CHANGED
@@ -28,6 +28,166 @@
28
28
 
29
29
 
30
30
 
31
+
32
+
33
+ これが他人の記事を閲覧できないようにする方法です。
34
+
35
+
36
+
37
+ ```PHP
38
+
39
+ if (!current_user_can('level_10')) {
40
+
41
+ function exclude_other_posts( $wp_query ) {
42
+
43
+
44
+
45
+ if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) {
46
+
47
+ $post_type = get_post_type_object( $_REQUEST['post_type'] );
48
+
49
+ $cap_type = $post_type->cap->edit_other_posts;
50
+
51
+ } else {
52
+
53
+ $cap_type = 'edit_others_posts';
54
+
55
+ }
56
+
57
+
58
+
59
+ if ( is_admin() && $wp_query->is_main_query() && ! $wp_query->get( 'author' ) && ! current_user_can( $cap_type ) ) {
60
+
61
+ $user = wp_get_current_user();
62
+
63
+ //※
64
+
65
+ $wp_query->set( 'author', $user->ID );
66
+
67
+ }
68
+
69
+
70
+
71
+ }
72
+
73
+ add_action( 'pre_get_posts', 'exclude_other_posts' );
74
+
75
+ }
76
+
77
+ ```
78
+
79
+
80
+
81
+ //※
82
+
83
+ set($query_var, $value)
84
+
85
+ 指定されたクエリ変数を任意の値に設定する。
86
+
87
+
88
+
89
+ たぶん、管理者(author)から今アクセスしているユーザーのIDに
90
+
91
+ 該当する記事を返しているので閲覧制限を実現できているのかと思います。
92
+
93
+
94
+
95
+ コメントも現在閲覧しているユーザーのIDを元にした
96
+
97
+ 記事を表示できればいいので
98
+
99
+
100
+
101
+ ```PHP
102
+
103
+ if (!current_user_can('level_10')) {
104
+
105
+
106
+
107
+ function exclude_other_comments( $wp_query ) {
108
+
109
+
110
+
111
+ if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) {
112
+
113
+ $post_type = get_post_type_object( $_REQUEST['post_type'] );
114
+
115
+ $cap_type = $post_type->cap->edit_other_posts;
116
+
117
+ print "パターン1";
118
+
119
+ } else {
120
+
121
+ $cap_type = 'edit_others_posts';
122
+
123
+ print "パターン2";
124
+
125
+ }
126
+
127
+
128
+
129
+ if ( is_admin() && $wp_query->is_main_query() && ! $wp_query->get( 'author' ) && ! current_user_can( $cap_type ) ) {
130
+
131
+ $user = wp_get_current_user();
132
+
133
+ $wp_query->set( 'author', $user->ID );
134
+
135
+ print "パターン3";
136
+
137
+ }
138
+
139
+
140
+
141
+ $wp_query->set( 'author', 3 );
142
+
143
+ print "実行";
144
+
145
+ }
146
+
147
+
148
+
149
+ add_action( 'pre_get_comments', 'exclude_other_comments' );
150
+
151
+ }
152
+
153
+ ```
154
+
155
+
156
+
157
+ pre_get_commentsをフックに同じように記述しているのですが、
158
+
159
+ 「パターン2」の処理にしかなりません。
160
+
161
+
162
+
163
+ 「パターン1」を通らないということは、
164
+
165
+ $_REQUEST['post_type']の段階で問題があるということかと思います。
166
+
167
+ POSTに問題があるということがいまいちイメージできないので
168
+
169
+ $_REQUEST['post_type']の中身を調べようと思いましたが、
170
+
171
+ 出力の方法が分からず断念しました。
172
+
173
+
174
+
175
+ また、最終的にcurrent_user_can()からユーザーのIDを取得し
176
+
177
+ 該当ユーザーの記事から更にコメントのついている記事を表示できる
178
+
179
+ クエリにすればいいと思うので
180
+
181
+ $wp_queryの中身を調べ、試行錯誤でクエリを組み立てようと思いましたが、
182
+
183
+ こちらも出力の方法が分からず断念しました。
184
+
185
+
186
+
187
+
188
+
189
+
190
+
31
191
  やり方が分かる方がいらっしゃいましたら
32
192
 
33
193
  お教え願えますでしょうか?