I am currently attempting to make comments that only the author when logged in can see those posts. Two different logins to the website cannot see each other's posts.
I have found some stuff online like a function to attempt this. However, every time I use this it breaks the site. I have done some research on applying filters and using the WP hook to do so. I unfortunately haven't been able to accomplish this.
Can this be done just by altering the select where
clause? To filter database off of user id and then capturing logged in user id through php
?
//After the comments are fetched, you can modify the comments array
add_filter('the_comments', 'wpse56652_filt_comm');
function wpse56652_filt_comm($param) {
global $current_user;
get_currentuserinfo();
//current users id = $current_user->ID;
//Current users posts, check get_posts params to change as per your need
$user_posts = get_posts(array('author' => $current_user->ID, 'posts_per_page' => -1));
echo '<pre>';
print_r($param);
echo '</pre>';
return $param;
}
I am currently attempting to make comments that only the author when logged in can see those posts. Two different logins to the website cannot see each other's posts.
I have found some stuff online like a function to attempt this. However, every time I use this it breaks the site. I have done some research on applying filters and using the WP hook to do so. I unfortunately haven't been able to accomplish this.
Can this be done just by altering the select where
clause? To filter database off of user id and then capturing logged in user id through php
?
//After the comments are fetched, you can modify the comments array
add_filter('the_comments', 'wpse56652_filt_comm');
function wpse56652_filt_comm($param) {
global $current_user;
get_currentuserinfo();
//current users id = $current_user->ID;
//Current users posts, check get_posts params to change as per your need
$user_posts = get_posts(array('author' => $current_user->ID, 'posts_per_page' => -1));
echo '<pre>';
print_r($param);
echo '</pre>';
return $param;
}
Share
Improve this question
edited Dec 10, 2019 at 18:42
cjbj
15k16 gold badges42 silver badges89 bronze badges
asked Dec 10, 2019 at 15:17
AndyAndy
212 bronze badges
1
- Can you provide clarification? Are you filtering comments or are you filtering posts? Are you in the admin panel or on the back end? Both? Is the logged in user only allowed to view their posts? – jdp Commented Dec 10, 2019 at 17:11
1 Answer
Reset to default 1The filter that you mention modifies the result of the get_comments
function after it has been fetched. However, you can also parse arguments to this function. You can see which arguments here. As you can see you can limit results to comments from a certain author or exclude them.
Now, here is where your question becomes a bit sketchy. It's not clear whether you want only comments of the current author to show, or all comments except those by authors who are not the current author (meaning: comments by current authors and comments made by people who are not authors).
The first is pretty straightforward. Fetch the current author and limit results accordingly.
$current_user_id = get_the_author_meta('ID');
if (!empty($current_user_id))
$current_comments = get_comments (array('post_id' => get_the_ID(), 'post_author__in' => array($current_user_id)));
The second is slightly more complicated. You will have to fetch an array of all authors, except the current user from and the get all comments excluding all other registered authors.
$current_user_id = get_the_author_meta('ID');
if (!empty($current_user_id)) {
$non_current_user_ids = get_users (array ('exclude' => array($current_user_id)));
$current_comments = get_comments (array('post_id' => get_the_ID(), 'post_author__not_in' => array($current_user_id)));
}
Note: I did not test the code. It is given here as a proof of concept.
Your question does not cover what happens when a user is not logged in. Does he get to see all comments or only those by non-registered users?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744920178a4601084.html
评论列表(0条)