How to search for post content and attached file names

If you look at the $wpdb->posts table in the database, you will notice that the file names, minus the file type ending, are used for the post_title for attachments. This means that you can effectively search the file name if you can get your search function to search attachments, which the default search (almost) already … Read more

Automatically deactivate link for attachments

With markup embedded into the content there is no less tricky way than regex to do it, save perhaps a markup parser like SimpleHTMLDom function remove_image_link_102512($content) { $pattern = ‘|<a.*?href=”https://wordpress.stackexchange.com/questions/102512/(.*)”.*>?(<img.*?/?>)(?:</a>)?|’; $content = preg_replace($pattern,’$2′,$content); return $content; } add_filter(‘the_content’,’remove_image_link_102512′); As with all regex + markup solutions, I’d test that thoroughly. Of course, as no saved data is … Read more

How do i get post thumbnail using attachment code

I can’t tell if you want a link to the thumbnail or you want the image to be the thumbnail. To get the image assigned to the post as the thumbnail image, use get_the_post_thumbnail: $thumb = get_the_post_thumbnail($post_id, ‘thumbnail’); To get the thumbnail using the attachment ID use wp_get_attachment_image: $thumb = wp_get_attachment_image( $attachment_id, ‘thumbnail’ ) Not … Read more

How to get the cropped image from media library?

Get the attachment ID for the image, then get the post meta field _wp_attachment_metadata for it. This is a serialized array. The field sizes in that array contains all available image sizes. Pseudo-code: $sizes = array(); $attachment_url=”http://examle.com/wp-content-uploads/foo.png”; $attachment_dir = dirname( $attachment_url ); $attachment_id = get_attachment_id( $attachment_url ); $attachment_meta = get_post_meta( $attachment_id, ‘_wp_attachment_metadata’, TRUE ); $sizes[] … Read more

Remove attachment from database

Just use a custom $wpdb query, something like function remove_all_attachments() { global $wpdb; $ids = $wpdb->get_col(“SELECT ID FROM $wpdb->posts WHERE post_type=”attachment””); if ( empty($ids) ) return; $remove = implode(‘,’, $ids); // remove post meta $wpdb->query(“DELETE FROM $wpdb->postmeta WHERE post_id IN ({$remove})”); // remove posts $wpdb->query(“DELETE FROM $wpdb->posts WHERE ID = IN ({$remove})”); } After that … Read more

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