Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get comment author's custom post meta WordPress

  • SOLVED

I want a snippet that will display the meta of a commenter's custom post (e.g. <?php the title(); ?>) in comments.php.

So, if the commenter has published a post of the custom post type XYZ then allow me to display that commenter's custom post.




/******************************************* Template for comments and pingbacks. */

if ( ! function_exists( 'generate_comment' ) ) :
function generate_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
$args['avatar_size'] = apply_filters( 'generate_comment_avatar_size', 50 );

if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>

<li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
<div class="comment-body">
<?php _e( 'Pingback:', 'generatepress' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', 'generatepress' ), '<span class="edit-link">', '</span>' ); ?>
</div>

<?php else : ?>

<li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="comment-meta">
<?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
<div class="comment-author-info">
<div class="comment-author vcard">
<?php if ($comment->user_id) {
$user=get_userdata($comment->user_id);
echo $user->user_firstname;
echo "&nbsp;";
echo $user->user_lastname;
} else { comment_author_link(); } ?>
</div><!-- .comment-author -->

<div class="entry-meta comment-metadata">
<time datetime="<?php comment_time( 'c' ); ?>">
<?php
comment_reply_link( array_merge( $args, array(
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<span class="reply">',
'after' => ' &bull; </span>',
) ) );
?>



<?php
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'author' => $comment->user_id,
'orderby' => 'post_date',
'order' => 'ASC',
'posts_per_page' => 1
);

$comment_author_posts = get_posts( $args );

if ($comment_author_posts):
foreach ( $comment_author_posts as $comment_author_post ) : setup_postdata( $comment_author_post ); ?>

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

<?php endforeach; wp_reset_postdata(); endif;?>


<?php printf( _x( '%1$s at %2$s', '1: date, 2: time', 'generatepress' ), get_comment_date(), get_comment_time() ); ?>
</time>
</div><!-- .comment-metadata -->
</div><!-- .comment-author-info -->

<?php if ( '0' == $comment->comment_approved ) : ?>
<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'generatepress' ); ?></p>
<?php endif; ?>
</footer><!-- .comment-meta -->

<div class="comment-content">
<?php comment_text(); ?>
</div><!-- .comment-content -->


</article><!-- .comment-body -->

<?php
endif;
}
endif; // ends check for generate_comment()

Answers (1)

2016-12-23

mod mi answers:

You can get the posts from the specific custom post type by the specific author. If any posts exist display the title of the latest like this:


$args = array(
'post_type' => array( ‘XYZ’ ),
'post_status' => array( 'publish' ),
'author' => $comment->user_id,
'orderby' => 'post_date',
'order' => 'ASC',
'posts_per_page' => 1
);

$comment_author_posts = get_posts( $args );

if ($comment_author_posts):
foreach ( $comment_author_posts as $comment_author_post ): ?>

<a href="<?php echo get_permalink( $comment_author_post->ID ) ?>">
<?php echo $comment_author_post->post_title ?></a>

<?php endforeach; endif;?>


pjeaje comments:

Doesn't seem to work... I need this to be inserted into my comments.php


mod mi comments:

Do you place this in the loop? It would be helpful if you could please post the full code of your comments.php template


pjeaje comments:

This is showing the current post's meta not the commentor's custom post meta


pjeaje comments:

see my edits... and where I have placed your code


mod mi comments:

Ok I've amended my answer, it should work now, please try the updated code in my answer


pjeaje comments:

Great work!