Home › Forums › General Issues › Custom comment field, not to display on reply
I added a custom field to comments which work and displays on comments as expected.
Problem is I don’t want the field to be shown again when a user replies to a comment. Is there a workaround?
Are you able to post the code? I’m thinking that you use count and only display the field if the result is equal to one.
So, if ( user comment count == 1 ) { echo $field }.
This will remove the field if the person has already made any comment on the current post.
add_filter('acf/prepare_field/name=YOUR_FIELS_NAME', 'remove_if_previous_comments');
function remove_if_previous_comments($field) {
if (!is_user_logged_in()) {
return $field;
}
global $post;
$user = get_current_user_id();
$args = array(
'author__in' => array($user),
'post_id' => $post->ID,
'count' => true
);
$count = get_comments($args);
if ($count) {
$field = false;
}
return $field;
}
unfortunately you cannot remove the field depending on if the comment is a reply to another comment or not. WP only has one comment form and it’s just moved around when replying to other comments.
@hube2 thank you!
I have a similar case: I have a set of nine mandatory fields in comments, and I need to hide all of them to users who have already posted a comment. So, the following comments from the same users would be only basic text with no custom fields.
Can I put an array in the first line of code, or the set name, or what?
Thanks in advance
// array of field names
$fields = array(
'name',
'name',
'etc'
)
foreach ($fields as $field) {
add_filter('acf/prepare_field/name='.$field, 'remove_if_previous_comments');
}
The topic ‘Custom comment field, not to display on reply’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.