0

How can I check if the current loop is a comment? Something like is_comment().

I'm building a custom update_x_meta() function where we update meta data according to the type, example:

if ( is_single() || is_attachment() || is_page() ) {
    // post, page, attachment
    update_post_meta($id, $meta_data, $meta_value);
} elseif (is_author()) {
    // author page
    update_user_meta($id, $meta_data, $meta_value);
} elseif ( is_category() || is_tag() || is_tax() ) {
    // taxonomies
    update_term_meta($id, $meta_data, $meta_value);
} elseif ( is_comment() ) {
    // comment
    update_comment_meta($id, $meta_data, $meta_value);
}

I suppose in order to make this work, I will need to target the conditional tags with a specific ID, like is_single($id) etc. although that may not work given that we can have the same ID for posts, terms...

I feel there should already be an update_x_meta() type of function in core already.

  • Hm, I haven't seen a comment page yet (other than is_comments_popup()). Did you mean to check if you are on a WP_Comment_Query loop? – Jack Johansson Jun 12 '17 at 13:34
  • @JackJohansson Yes, that is exactly what I mean. – Christine Cooper Jun 12 '17 at 13:36
  • Although on second thought, the if statement in its current form wouldn't work regardless... because is_single() will be true even if we are on a WP_Comment_Query loop. I'm rethinking this approach. Maybe a global $post route... – Christine Cooper Jun 12 '17 at 13:41
  • I was thinking about the same think for the past 5 minutes. The WP_Query doesn't query the comments, so you have to either query the comments manually by WP_Comment_Query or check to see if it the post has any comments, and then run the loop. Otherwise, i don't know when is_comment() can be true, since comments are manually pulled by comments_template(); Whenever needed. – Jack Johansson Jun 12 '17 at 13:44
  • I could do something like global $wp_query; and then use conditions like $wp_query->is_page etc. The problem is still the same though I guess. Back to the drawing board... =( – Christine Cooper Jun 12 '17 at 13:49
1

The comments popup page contains a loop of the comments, which can be checked by an is comments popup() conditional, that returns either true or false. However, i don't see this feature being used in modern development. This might be the only place containing a direct loop of comments.

The $wp_query itself does not contain any comment data, except whether the post has any comments or not (and how many of them exist). The comment themselves are called later in the template files (such as single.php) by using a loop like this:

while ( have_posts() ) { 
    the_post(); 
    get_template_part( 'SOME TEMPLATE HERE' ); 
    comments_template();
}

The comments_template() itself will use WP_Comments_Query() to query a list of comments. So, to be on a comment loop means to be on a page or post template. So the conditional posted in the question won't help, unless we change it like this:

if ( is_single() || is_attachment() || is_page() ) {
    // post, page, attachment
    update_post_meta($id, $meta_data, $meta_value);
    if(have_comments()) {
        // Run some stuff here
    }
} elseif (is_author()) {
    // author page
    update_user_meta($id, $meta_data, $meta_value);
} elseif ( is_category() || is_tag() || is_tax() ) {
    // taxonomies
    update_term_meta($id, $meta_data, $meta_value);
}

Which will update both comments and posts at the same time.

But there is a filter, which will trigger whenever comment_template() function is called in the template files, so it might be a place to hook and update the comments meta:

add_filter( "comments_template", "update_comments_meta" );
function update_comments_meta($comment_template){
    global $post;
    if ( have_comments() ){
        // Do some stuff here
    }
    return $comment_template;
}
  • Thank you for posting this Jack +1 -- I am going to see if I can find a more direct solution to this. It's a task I have lined up in a few weeks. If nothing profound is found, I will accept this. I like the idea of hooking into comment_template(). – Christine Cooper Jun 17 '17 at 11:35
  • @ChristineCooper You're welcome. I couldn't find anything more direct direct in neither codex nor code reference. However, others might be able to provide an answer if you bump the post to home or leave a message in the chat. – Jack Johansson Jun 17 '17 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.