How to get all the attachments in WordPress |
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, PHP and Data Analyst. Database Management Advance Level

Comment Below

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

*

Current ye@r *

Menu

Sidebar