2016-01-16 56 views
0

仍然學習,感覺我只有幾步之遙,所以我感謝您的幫助!WordPress的/ PHP的get_attachment_url基於uid和後父母的ID

有多個子帖子應該根據子帖子上傳的內容返回一個附件url。使用數組收集上傳的附件。我以某種方式設法使其工作,但它會返回從該用戶上傳的所有內容,當我希望它只返回來自該用戶的最新上傳內容時。任何使用'max'來完成這個似乎都會引發一個錯誤。從底部開始的第五行是麻煩點。我正在使用post_date嘗試完成此操作,但我認爲ID也足夠了。感謝您提供任何輸入。

  $exclude = array(); 

     $args = array(
      'order'   => 'ASC', 
      'post_type'  => 'attachment', 
      'post_mime_type' => 'image', 
      'post_parent' => $pid, 
      'numberposts' => -1, 
      'post_status' => null, 
     ); 

     $attachments = get_posts($args); 

     foreach($attachments as $att) $exclude[] = $att->ID; 

     $args = array(
      'order'   => 'ASC', 
      'post_type'  => 'attachment', 
      'post_parent' => $pid, 
      'post_author' => $uid, 
      'ID'    => $pkey, 
      'exclude' => $exclude, 
      'numberposts' => -1, 
      'post_status' => null, 
     ); 
     $attachments = get_posts($args); 



     if(count($attachments) == 0) echo __('No project files.','ProjectTheme'); 

        foreach($attachments as $at) { 
        if($at->post_author == $user->ID) { 
         if($at->post_parent == $row->pid) { 
          if($at->post_date == $at['max(post_date)']) { 
          $fileName = wp_get_attachment_url($at->ID); 
          echo '<div>'. mp3j_put('[mp3j track="' . $fileName . '" flip="y"]').'</div><br>'; 
             }}}} 
              ; 
+0

這是wordpress嗎? – cklm

回答

0

我最終通過下面的解決方案使用SQL來解決這個問題。

$audiolinks = "select ID, post_author, post_parent, max(post_date), guid 
    from ".$wpdb->prefix."posts 
    where post_author='$user->ID' AND post_parent ='$pid' order by id DESC"; 
$audiolink = $wpdb->get_results($audiolinks); 
$filelink = wp_get_attachment_url($audiolink[0]->ID); 
$fileauthor = $user->user_login; 
$filetitle = $post->post_title; 
echo '<div style="padding-top: 10px">' 
    . mp3j_put('[mp3j track="' . $filelink . '" title="Play Audition: ' 
    . $fileauthor . " - " . $post->post_title 
    . '" flip="y"]').'</div><br>';