Tag Info

Hot answers tagged

5

If you're comfortable with the complete opposite of the linked answer, you simply could use this query to fetch all posts that have an attachment and then use their IDs as the post__not_in parameter of \WP_Query: $attachment_args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', ...


3

I got curious about the SQL way of finding all posts without any attachments. Method #1 - Sub query with NOT IN Here's my first attempt to construct such a query: global $wpdb; $sql = " SELECT p1.ID, p1.post_title FROM {$wpdb->posts} p1 WHERE p1.post_type = 'post' AND p1.post_status = 'publish' ...


3

This should hopefully do the trick: /** * Append the image categories to the current image class. * * @see http://wordpress.stackexchange.com/a/156576/26350 */ add_filter( 'get_image_tag_class', function( $class, $id, $align, $size ) { foreach( (array) get_the_category( $id ) as $cat ) { $class .= ' category-' . ...


2

This should work for the rel attribute: /** * Create a rel attribute from the image categories * * @see http://wordpress.stackexchange.com/a/158024/26350 */ add_filter( 'get_image_tag', function( $html, $id ) { $rel = array(); foreach( (array) get_the_category( $id ) as $cat ) { $rel[] = $cat->slug; ...


2

If you want to use get_attached_media(), you can try for example: if( has_post_thumbnail() ) { the_post_thumbnail(); } else { $imgs = get_attached_media( 'image' ); if( count( $imgs ) > 0 ) { $img = array_shift( $imgs ); echo wp_get_attachment_image( $img->ID, 'thumbnail' ); } } to display one of the attached ...


1

You can use add_attachment action to set a default value for a custom field: function set_default_meta_value_62481( $attachment_id ) { add_post_meta( $attachment_id, '_show_attach', '1' ); } add_action( 'add_attachment', 'set_default_meta_value_62481' );


1

I ended up modifying slightly to use data-filter instead of rel. Works perfectly in combination with the isotope j-query plugin. add_filter( 'get_image_tag', function( $html, $id ) { $dataFilter = array(); foreach( (array) get_the_category( $id ) as $cat ) { $dataFilter[] = $cat->slug; } ...


1

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 any other place (sidebar widget?) or maybe by ...



Only top voted, non community-wiki answers of a minimum length are eligible