matching items in two lists
-
I really don’t understand what the code you posted is trying to do. Can you better explain in words? Do you want to loop through the approved comments for this post and just show one per author?
Also, I don’t see a list of authors, I only see a display of coauthors nicknames and another display of their IDs. I don’t even know how that works – it looks like the ID list would follow the nickname list.
It looks like you may be using the plugin Co-Authors, and I don’t see any documentation that would let you get a list from that plugin, only display the data. Does the plugin include a function get_coauthors_IDs()?
Sorry – it made more sense in my head as I wrote it!!
But you have the idea. I want to show only the most recent comment from one of the authors.
The multiple authors are made possible from the plugin Co Authors Plus.
The plugin does have a few functions, and I think that: get_coauthors( [$post_id], [$args] ) is probably what we need.
Here is the wordpress plugin page that gives more details: http://wordpress.org/extend/plugins/co-authors-plus/other_notes/
Thanks!
OK, here is some code to match the two arrays.
<?php global $post; $comments = get_approved_comments($post->ID); $coauthors = get_coauthors($post->ID); $coauthors_by_ID = array(); foreach ($coauthors as $coauthor) { $coauthors_by_ID[$coauthor->ID] = $coauthor; } $comment_matches = array(); for ($i = 0; $i < sizeof($comments);++$i) { $comment = $comments[$i]; if ($coauthors_by_ID[$comment->user_id]) { $comment_matches[$comment->user_id] = $i; //unset($coauthors_by_ID[$comment->user_id]); // Uncomment this to get the first match } } ?>
$comment_matches is an array keyed on coauthor ID, with the values being the index numbers of the coauthor’s comment. So, for example you could print each comment with this code:
foreach ($comment_matches as $key => $value) { $author = $coauthors_by_ID[$key]; echo "<p>Comment by: {$author->display_name}</p>"; $comment = $comments[$value]; echo "<p>{$comment->comment_content}</p>"; } ?>
Hi,
This is great – but not quite right just yet!
At the moment I see the most recent reply from all the co-authors, where what I need is just 1 (i.e. the one latest comment that is from an author).
The site is a work in progress (obviously) but if it helps you can see it here: http://www.photoshoppingpong.com/cms/
Thanks for all your help
How do you decide which author?
It doesn’t matter which author, just that it is the most recent reply from one of them. (actually if we could ignore the comments from user_id=1 that would be fantastic too!)
(oh and I moved the blog url to the root: http://www.photoshoppingpong.com )
Let me see if I understand. You want to get the single most recent comment from any one of the coauthors, except excluding user_id=1. Does that sound right?
that is it exactly!
OK, give this a try.
<?php global $post; $comments = get_approved_comments($post->ID); $coauthors = get_coauthors($post->ID); $coauthors_by_ID = array(); foreach ($coauthors as $coauthor) { if ($coauthor->ID == 1) continue; $coauthors_by_ID[$coauthor->ID] = $coauthor; } $comment_matches = array(); for ($i = sizeof($comments)-1; $i > -1;--$i) { $comment = $comments[$i]; if ($coauthors_by_ID[$comment->user_id]) { $comment_matches[$comment->user_id] = $i; break; } } foreach ($comment_matches as $key => $value) { $author = $coauthors_by_ID[$key]; echo "<p>Comment by: {$author->display_name}</p>"; $comment = $comments[$value]; echo "<p>{$comment->comment_content}</p>"; } ?>
You are a star! It works like a charm. Thank you very very much.
Glad it worked! Now, please use the dropdown at top right to mark this topic ‘Resolved’.
Done!
- The topic ‘matching items in two lists’ is closed to new replies.
(@nathan12343)
14 years, 2 months ago
I have a list of authors and a list of comment authors for a given post. What I want to be able to do is show the latest comment made by an author. Can anyone help me? Well out of my php depth!
This is the various bit of code I am using. All this inside the loop.