I call it when I render a metabox on the edit page. (
Yeah, you shouldn’t be using globals in this context – use a foreach
loop instead:
function get_available_attachments( $post /* This is the post object being edited */ ) {
$attachments = [];
$attach_query = new WP_Query([
'post_type' => 'attachment',
'post_status' => 'inherit', // default is 'publish'
'post_mime_type' => 'application/pdf',
'orderby' => 'name',
// 'orderby' => 'date',
'order' => 'DESC',
]);
foreach ( $attach_query->posts as $att ) {
$attachments[] = ( object ) [
'mime_type' => $att->post_mime_type,
'title' => $att->post_title,
'ID' => $att->ID
];
}
return $attachments;
}
Related Posts:
- Broken? WP_Query and “attachment” as a post type
- Is it possible to query all posts that don’t have an attachment?
- How to paginate wordpress [gallery] shortcode?
- How to get Page/Post Gallery attachment images in order they are set in backend using WP_Query()?
- Getting attachments by meta value
- Count the number of images uploded on the website
- How to paginate attachments in a secondary query as gallery?
- How do I exclude all images from a wp_query?
- get images attached to post
- Get attached media only
- query attachments of parent page if attachments of current page are smaller than …
- wp_query for displaying attachments with a tag
- Show selected images on top in Media Manager
- How can I query posts with newly uploaded images?
- Sorting By Custom Posts With Attachments
- How to fetch only media that was already attached to a post/page?
- How to do meta_query for attachments?
- Get attachment by meta_key value
- How to show optimized list of posts with all their attachment images
- Where to put meta Keys
- WP_Query Attachment adds additional attachment count and need to link attachment to post url
- Why having more than 10 clauses in WP_Query results in some outputs being dropped?
- wp_query get attachments in larger size
- Creating attachments archive in tags and categories
- Ignore image urls in wp_query search
- WP_Query: attachment image in “full” size?
- How can I build a query that returns all attachments of a page and it’s children pages?
- get all images from the wordpress media library with link to the post they are associated with
- How to restrict add media library only to images of the same post family?
- how to get custom attachment url?
- wp_get_attachment_image not to get the post_thumbnaill
- Query All Attachments and Order by Parent Publish Date
- How To Query All Attachment Images Found In Post Galleries
- WP_Query for attachments without duplicating post_parent and displaying tagged image
- Using WP_Query to get attachment returns empty set
- Some images not being returned with wp_get_attachment_image
- Loop issues when creating custom query for media uploader
- Give attachments an archive page, and exclude unattached ones
- on attachment.php, how to display previous and next attachment links that follow the same order as a custom WP Query
- Order by DESC, ASC in custom WP_Query
- Exclude or Include category ids in WP_Query
- Secondary Sort (fallback) for WP_Query
- How to stop wordpress to make the default query?
- WP_Query based on URL parameters
- Order by meta_value_num DESC and meta_value ASC on WP 4.0
- Order posts ascending with number in title
- How can I style future post?
- using wp_query to return posts w/ comment count > 0
- Order by meta_key in custom post type doesn’t affect the query
- Is there a PHP function that can match anything after the given URL, so my IF statement will work in each instance?
- Add a default meta_value to new posts
- WP query exclude post within the last month / only show over 1 month old
- WordPress query for most recent posts from multiple categories
- Order Posts by meta value AND published date
- Pass array of taxonomy terms to wp_query
- How to display upcoming events by dat with Modern Events Calendar Lite
- Wp_query Add specific pending posts
- How to Filter Posts by Custom Fields?
- pagination with ‘no_found_rows’ => true,
- How to get all product attributes for filters in WooCommerce?
- Fetch Record based on meta key dates
- Query posts from category A, and from either category B or C
- Query posts with “non set” meta value
- Add filter post_where and passing post_type argument
- WP_Query infinite Loop
- Advanced WP Query and/or
- 2 wordpress loops showing 1 post from same post type – how to avoid showing the same post?
- WP Query – order posts by meta field first and then order the rest
- Reusing content from front page on sub page
- Get Child Page IDs by Parent ID
- Wildcard search in WP Query
- Pagination works in custom query loop but it doesn’t work in shortcode
- Create a notification for post field
- How to query posts for custom post type with featured image for first 2 post?
- Why is get_the_excerpt returning full content
- How to avoid filling up an array each time I run a WP_query?
- Randomly pick one testimonial post
- Custom post type – filter content by custom meta box content
- Trying to output content from page, works locally but not online
- Admin: Custom Query Returning Permissions Error
- Combine relationship posts with existing wp_query
- Slick slider for post doesn’t display anything
- What is the equivalent WP_Query of a SQL Query?
- Count post meta entries
- use mysql variable in a $wpdb->query to reindex a column?
- Query WP Posts, then list the taxonomies from those posts
- Query post with content only
- Search with WP_Query, but ignore href URLs in anchor tags?
- Displaying Pages in Nav Sub-Menu with Specific Taxonomy Terms?
- Is meta_key used for ordering, filtering, or both?
- compare for multiple meta values for same key in wp_query
- Taxonomy Terms That Don’t Exist Display Results
- Why is my query not giving results?
- Adding a Theme Customizer Control for Posts_Per_Page
- Custom query from all posts & all child page from one parent page?
- wp_query conditionally get posts
- the_excerpt() not working in custom archive
- WordPress extremely slow when using get_posts with multiple meta_query relations
- Refresh wp_query with no changes
- Search results stuck on page 1