List all posts commented by current user

I need a page to display all posts (in Foods custom post type) that have comments by the logged in user.

The list just needs to show the title with link to the title.

Is this possible?

What I’ve tried so far:

<?php if (is_user_logged_in() ) : ?>

<?php
        if ( is_user_logged_in()) {

        global $current_user;
        get_currentuserinfo();

        $args=array(
            'post_type' => 'foods',
            'posts_per_page' => 5,
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
        echo 'Your Comments';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="https://wordpress.stackexchange.com/questions/160598/<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
        endwhile;
        }
        wp_reset_query();  // Restore global post data stomped by the_post().
        }
    ?>
<?php endif; ?> 

But of course I haven’t used $current_user->ID yet. I’m not sure yet of how to use it here.

2 s
2

If we want to avoid any filters and manual SQL queries, we could try (untested):

$args = array(
    'post_type'      => 'foods',
    'posts_per_page' => 5,
    'post__in' => array_unique( 
         wp_list_pluck( 
            get_comments( array(
                'user_id' => get_current_user_id() 
                )
            ),       
            'comment_post_ID' 
         )
    ),
);
$my_query = new WP_Query( $args );

Leave a Comment