How to get all the attachments in WordPress - Kvcodes
X

Congrats, You are Subscribed to Receive Updates.

How to get all the attachments in WordPress


How to get all the attachments in WordPress. Getting all attachment is good one for the common operations with media files.  Let’s try coding the feature to get all the attachments  which attached with a post.

 $args = array('post_type'=>'attachment','numberposts'=>null,'post_status'=>null);
   $attachments = get_posts($args);
    if($attachments){
          foreach($attachments as $attachment){
             // here your code 
              echo $attachment->ID; 
           }
      }

And if you want to a posts all attached images or medias, you can try the below code.

 $args = array('post_type'=>'attachment','numberposts'=>null,'post_status'=>null, 'post_parent' => $post->ID);
   $attachments = get_posts($args);
    if($attachments){
          foreach($attachments as $attachment){
             // here your code 
              echo $attachment->ID; 
           }
      }

That’s it.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.

Advertisement

Category Posts