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() ) : ?>

        if ( is_user_logged_in()) {

        global $current_user;

            '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="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        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.


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( 
            get_comments( array(
                'user_id' => get_current_user_id() 
$my_query = new WP_Query( $args );
  • 1
    Works like magic. I added array() in get_comments. – Bernie Sep 8 '14 at 12:25
  • is there any way to get this for the liked posts via custom field? "Current User Liked Videos" – Gazi Dec 10 '18 at 11:30
  • @Gazi it sounds like you are looking for the meta_query attribute in WP_Query if you need to search for posts with a given post meta (custom fields). See e.g. here in the (old) Codex. Note that in general the meta queries might not scale well if you have many thousands of posts. – birgire Dec 10 '18 at 11:55
  • yea I know 'meta_key' =>'positive_like', but I don't know how to integrate into your array_unique( section. I'm sorry for replying ona resolved topic but looks like the only one that can give the solution. – Gazi Dec 10 '18 at 13:28
  • @Gazi I think there's still some info needed on the full detail to answer your question, but I would suggest to ask a new question, with all the details included, as the comment system here will prompt a warning on long comment conversation and maybe others would gain from such a question :-) – birgire Dec 10 '18 at 14:29

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.