How to get attachments for a specific post type?

$posts = get_posts( array( ‘post_type’ => ‘post’, ‘posts_per_page’ => -1 ) ); foreach ( $posts as $post ) { $attachments = get_children( array ( ‘post_parent’ => $post->ID, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘post_per_page’ => 25 ), ARRAY_A ); if( $attachments ) { //continue with what you were doing } }

Unattaching unlinked images

wordpress sucks in keeping media<=>content relationships. Part of the problem is that by default all media are public once they are uploaded and you have no way to know where are they are being used. Just because an image is not referenced anymore in its original post doesn’t mean that it is not referenced at … Read more

delete attachment from front end

ended up using ajax in the end… html; <a class=”remImage” name=”<?php echo $attachment->ID; ?>” href=”#”><?php _e(‘delete’);?></a> <input type=”hidden” id=”att_remove” name=”att_remove[]” value=”<?php echo $attachment->ID; ?>” /> <input type=”hidden” name=”nonce” id=”nonce” value=”<?php echo wp_create_nonce( ‘delete_attachment’ ); ?>” /> jquery $(‘.remImage’).live(‘click’, function() { var attID = jQuery(this).attr(‘name’); jQuery.ajax({ type: ‘post’, url: ‘/wp-admin/admin-ajax.php’, data: { action: ‘delete_attachment’, att_ID: jQuery(this).attr(‘name’), … Read more

Detect if is image unattached

For unattached images, the post_parent column in the wp_posts table, has the value of 0. Your gallery shortcode in that case is: which means you are calling get_children() with post_parent as 0. You can e.g. use the gallery shortcode callback gallery_shortcode() directly: if( $parent_id = $post->post_parent ) { echo gallery_shortcode( [ ‘id’ => (int) $parent_id, … Read more

media_sideload_image file name?

When I started using media_sideload_image(), I found it pretty useless as it returned a full html tag, instead of the attachment ID, so I created my own version of it which replicates all the functionality of media_sideload_image() and adds some very useful stuff, like: saving the sideloaded file under a completely new filename making the … Read more

Is it possible to query specific WordPress Attachment files (.ppt & .pdf) and output them in a list?

I don’t think it’s documented in Codex but quick search in code shows that you can query by mime type. For jpeg images that would be something like: $posts = get_posts( array( ‘post_type’ => ‘attachment’, ‘post_status’ => ‘any’, ‘post_mime_type’ => ‘image/jpeg’, ) ); Not sure what types WP uses for files you need, don’t have … Read more

Applying automatic link class to images embedded to posts

You can run a filter on image_send_to_editor, the filter runs inside the get_image_send_to_editor function which is resposible for sending the link HTML that surrounds images sent to the editor. The filter can be found in core, in wp-admin/includes/media and is linked below for quick reference. core.trac.wordpress.org/browser/tags/3.1/wp-admin/includes/media.php

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)