WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a code snippet to display attachments to a post but for each user, he should only be able to see his own attachments and not those of other users


    //display all post attachments. see http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post
    $args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
echo "<b><u>Uploads By me</u></b>"; 
echo "<ol>";
    $attachments = get_posts($args);
    if ($attachments) {

        foreach ($attachments as $attachment) {
            echo "<li>";
            the_attachment_link($attachment->ID, false);
            echo " by author ".$attachment->post_author ;//debug info
            echo " and Current user is ".$current_user->ID ;//debug info
            if($current_user->ID==$attachment->ID) echo " Matched!! ";//debug info
            echo "</li>";
echo "<ol>";
//end attacmnets

I placed the code in my custom page template and as you can see in picture below, the statement if($current_user->ID==$attachment->ID) echo " Matched!! "; failed and this is what i wanted to depend on to selectively show attachments.Please help outenter image description here

share|improve this question
up vote 2 down vote accepted

Maybe you meant:

if($current_user->ID == $attachment->post_author) echo " Matched!! ";

PS: By unexpected results I think you are referring to the 'DISEASES OF THE PENIS' part... right?

share|improve this answer
is 100% correct. In this case, $current_user->ID is the ID of the user. $attachment->ID is the ID of the attachment. Try to keep variable/object scoping in mind when you do your comparisons. – EAMann Oct 28 '11 at 3:41
@EAMann I still think it's the DOTP entry that caused unexpected results :) – EarnestoDev Oct 28 '11 at 3:42
I wasn't gonna go there :-) – EAMann Oct 28 '11 at 3:44
Oh sorry. Am going to edit picture :-) – Dr Deo Oct 28 '11 at 3:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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