質問編集履歴
1
現在の状況を具体的にした
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,6 +13,86 @@
|
|
13
13
|
コメント取得用のクエリを変更すれば良いと思うのですが、
|
14
14
|
なかなか方法が見当たらず躓いております。
|
15
15
|
|
16
|
+
|
17
|
+
これが他人の記事を閲覧できないようにする方法です。
|
18
|
+
|
19
|
+
```PHP
|
20
|
+
if (!current_user_can('level_10')) {
|
21
|
+
function exclude_other_posts( $wp_query ) {
|
22
|
+
|
23
|
+
if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) {
|
24
|
+
$post_type = get_post_type_object( $_REQUEST['post_type'] );
|
25
|
+
$cap_type = $post_type->cap->edit_other_posts;
|
26
|
+
} else {
|
27
|
+
$cap_type = 'edit_others_posts';
|
28
|
+
}
|
29
|
+
|
30
|
+
if ( is_admin() && $wp_query->is_main_query() && ! $wp_query->get( 'author' ) && ! current_user_can( $cap_type ) ) {
|
31
|
+
$user = wp_get_current_user();
|
32
|
+
//※
|
33
|
+
$wp_query->set( 'author', $user->ID );
|
34
|
+
}
|
35
|
+
|
36
|
+
}
|
37
|
+
add_action( 'pre_get_posts', 'exclude_other_posts' );
|
38
|
+
}
|
39
|
+
```
|
40
|
+
|
41
|
+
//※
|
42
|
+
set($query_var, $value)
|
43
|
+
指定されたクエリ変数を任意の値に設定する。
|
44
|
+
|
45
|
+
たぶん、管理者(author)から今アクセスしているユーザーのIDに
|
46
|
+
該当する記事を返しているので閲覧制限を実現できているのかと思います。
|
47
|
+
|
48
|
+
コメントも現在閲覧しているユーザーのIDを元にした
|
49
|
+
記事を表示できればいいので
|
50
|
+
|
51
|
+
```PHP
|
52
|
+
if (!current_user_can('level_10')) {
|
53
|
+
|
54
|
+
function exclude_other_comments( $wp_query ) {
|
55
|
+
|
56
|
+
if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) {
|
57
|
+
$post_type = get_post_type_object( $_REQUEST['post_type'] );
|
58
|
+
$cap_type = $post_type->cap->edit_other_posts;
|
59
|
+
print "パターン1";
|
60
|
+
} else {
|
61
|
+
$cap_type = 'edit_others_posts';
|
62
|
+
print "パターン2";
|
63
|
+
}
|
64
|
+
|
65
|
+
if ( is_admin() && $wp_query->is_main_query() && ! $wp_query->get( 'author' ) && ! current_user_can( $cap_type ) ) {
|
66
|
+
$user = wp_get_current_user();
|
67
|
+
$wp_query->set( 'author', $user->ID );
|
68
|
+
print "パターン3";
|
69
|
+
}
|
70
|
+
|
71
|
+
$wp_query->set( 'author', 3 );
|
72
|
+
print "実行";
|
73
|
+
}
|
74
|
+
|
75
|
+
add_action( 'pre_get_comments', 'exclude_other_comments' );
|
76
|
+
}
|
77
|
+
```
|
78
|
+
|
79
|
+
pre_get_commentsをフックに同じように記述しているのですが、
|
80
|
+
「パターン2」の処理にしかなりません。
|
81
|
+
|
82
|
+
「パターン1」を通らないということは、
|
83
|
+
$_REQUEST['post_type']の段階で問題があるということかと思います。
|
84
|
+
POSTに問題があるということがいまいちイメージできないので
|
85
|
+
$_REQUEST['post_type']の中身を調べようと思いましたが、
|
86
|
+
出力の方法が分からず断念しました。
|
87
|
+
|
88
|
+
また、最終的にcurrent_user_can()からユーザーのIDを取得し
|
89
|
+
該当ユーザーの記事から更にコメントのついている記事を表示できる
|
90
|
+
クエリにすればいいと思うので
|
91
|
+
$wp_queryの中身を調べ、試行錯誤でクエリを組み立てようと思いましたが、
|
92
|
+
こちらも出力の方法が分からず断念しました。
|
93
|
+
|
94
|
+
|
95
|
+
|
16
96
|
やり方が分かる方がいらっしゃいましたら
|
17
97
|
お教え願えますでしょうか?
|
18
98
|
|