Recover uploaded images in a Post

When we write a post in WordPress you can upload the images to the same time this article was written. These images, which are part of the library media, are associated with that particular post. The peculiarity of this behavior lies in the ability to extract these images regardless if they have been physically inserted inside the post. This procedure, therefore, is much more interesting than the use of the fields personallizzati (custom fields) or "incomprehensible" (for some) regexp (regular expressions) within the body of the Post. Recently, moreover, WordPress allows you to insert a simple, yet immediate, gallery, using their own link between this post and uploaded image. Low-level images uploaded are treated exactly like a post, that is, the database table and fields are exactly the same.
For example, if for a given post - with the loaded images - use the following code:

1
2
3
4
5
6
/ / Where is $ post_id is the id of the post
/ / If we are in a loop we can use $ post-ID
get_children ( 'post_type=attachment&post_mime_type=image&post_parent=' . $post_id ) ; $ To = & get_children ('post_type = attachment & post_mime_type = image & post_parent ='. $ Post_id);
$ai as $image ) { foreach ($ to as $ image) {
. $image -> post_title . '</p>' ; echo 'Into'. $ image -> post_title. '</ p>';
}

We get the list of titles of uploaded images. To view the images just modify the output:

1
. $image -> post_title . '<img src="' . $image -> guid . '" /></p>' ; echo 'Into'. $ image -> post_title. '<img src="'. $image -> guid.' "/> </ p> ';

However, it is more correct - for reasons of compatibility with future updates - use the features that WordPress offers to retrieve the url of the image. For example we can see both the image in real dimesioni the thumbnail image at:

1
2
3
. $image -> post_title . echo 'Into'. $ image -> post_title.
wp_get_attachment_thumb_url ( $image -> ID ) . '" />' . '<img Src="'. Wp_get_attachment_thumb_url ($image -> ID).' "/> '.
wp_get_attachment_url ( $image -> ID ) . '" /></p>' ; '<img Src="'. Wp_get_attachment_url ($image -> ID).' "/> </ P> ';

Sorting

In the examples that we have seen over the images are recovered without any specific order for a maximum reverse order of loading. If you have ever tried to use the gallery of WordPress, you'll notice that this allows you to sort the images loaded, the sort can be done through the title, date and time, or a random mode. In addition it is possible to use the manual mode, then we select an order of the images; always compared to the concept of the gallery, then the sequence (the first, the second, the third, and so on ...). You can also specify how increasing or decreasing the criterion we have chosen. In particular, the manual sorting, as it works for pages, ie exploiting the field menu_order (initially set to 0 for all uploaded images).
If you have loaded a series of images in a post and manipulated these with the manual sorting, we must make sure that our cycle is taken into account also the order. per qualsiasi tipo di campo ( menu_order , ID , data , etc…): A quick, and extremely simple to do this is to use the usort() to sort our array for any type of field ( menu_order , ID , data , etc ...):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/ / Where is $ post_id is the id of the post
/ / If we are in a loop we can use $ post-ID
get_children ( 'post_type=attachment&post_mime_type=image&post_parent=' . $post_id ) ; $ To = & get_children ('post_type = attachment & post_mime_type = image & post_parent ='. $ Post_id);

/ / Apply a sorting function so my
/ / You can choose your room the field on which to perform
/ / The sorting (in our case menu_order)
$ai , "sortImage" ) ; usort ($ ai, "sortImage");

$ai as $image ) { foreach ($ to as $ image) {
. $image -> post_title . '<img src="' . wp_get_attachment_thumb_url ( $image -> ID ) . '" /></p>' ; echo 'Into'. $ image -> post_title. '<img src="'. wp_get_attachment_thumb_url ($image -> ID).' "/> </ p> ';
}

$a , $b ) { sortImage function ($ a, $ b) {
$a -> menu_order == $b -> menu_order ) return 0 ; if ($ a -> menu_order == $ b -> menu_order) return 0;
$a -> menu_order > $b -> menu_order ) ? 1 : - 1 ; return ($ a -> menu_order> $ b -> menu_order)? 1: - 1;
/ / Return ($ a-> menu_order> $ b-> menu_order)? -1: 1, / / descending
}

Needless to say, of course, if you want to retrieve the "before" and one image loaded just delete the foreach() .

11 comments to: " "

  1. September 5, 2009 Best of the Week # 28 | BigThink :

    [...] Recovering the uploaded images in a Post A script required to restore the images of a post, even if these were not actually inserted in the text! [...]

  2. January 6, 2010 James:

    Hello and thank you very much for your article. Pero 'and I did not understand what' the code to insert want to retrieve only a preview of the first image inserted into the post ...

  3. January 7, 2010 Giovan Battista Fazioli :

    @ James: comment out the line 10:12 last example, eliminating the foreach . Instead of row 10, so before you echo ... enter:

    1
    $ai [ 0 ] ; // prendo la prima immagine solamente $ Image = $ to [0]; / / get the first image only

    I hope I have understood your question ...

  4. January 7, 2010 James:

    @ Giovan Battista Fazioli:
    Giovambattista Hello and thank you very much for your response. I entered the code as you have changed and there seems to be a problem on the usort function and also the image is not displayed ...
    Warning: usort() [function.usort]: Invalid comparison function. in /membri2/xxxxx/wp-content/themes/nomtema/index.php on line 27

    How come? use WP 2.9.1

  5. January 7, 2010 Giovan Battista Fazioli :

    @ James: it would seem that the function of sort sortImage() is not written correctly. In case put to me the entire code you have written, even in email.

  6. January 7, 2010 James:

    @ Giovan Battista Fazioli:

    I call the php code that I inserted via e-mail

    Thank you very much

  7. November 11, 2010 Giorgio:

    Hello,
    I would like to ask you information about the script, with the introduction of wp 3.0, they have added the ability to insert the thumbs, I would need to modify the script in such a way as not to appear in the image list, the one that is used as thumbs of the post.

    Do you know by any chance give me a hand?
    thank you very much in advance,
    Giorgio

  8. November 12, 2010 Giovan Battista Fazioli :

    @ George: you could do it this way. Before removing the images related to a post, extract any thumbnail (as indicated in WordPress 2.9 +: the new post thumbnails function ). Inside the loop of extraction, then, if the associated image corresponds to that of the thumbnail exclude. If I understand correctly ...

  9. November 12, 2010 Giorgio:

    Perfect, thank you! Among other things I thought to a solution of this type or alternatively thought to insert the thumb as the first image in the gallery and then to exclude through an artifice:

    1
    $ai == $ai [ 0 ] ) { } else { echo ... } if ($ to == $ to [0]) {} else {...}

    could be a solution equally effective in your opinion?

    Thank you very much for your answer,
    Giorgio

  10. November 12, 2010 Giovan Battista Fazioli :

    @ George: that's great also positioning it as the first, just remember :)

  11. November 12, 2010 Giorgio:

    Hello,
    I'm sorry if I continue to pester, but I get errors and can not understand why ...

    my function is the following:

    1
    2
    3
    4
    get_children ( 'post_type=attachment&post_mime_type=image&post_parent=' . $post -> ID ) ; $ To = & get_children ('post_type = attachment & post_mime_type = image & post_parent ='. $ Post -> ID);
    $ai as $image ) { foreach ($ to as $ image) {
    . $image -> post_title . '</p>' ; echo 'Into'. $ image -> post_title. '</ p>';
    }

    How do I let him do the checking to see if there is a post thumbail and then exclude it? In my template I have enabled support this function and everything works perfectly, I just have not figured out how to make him precisely control which element is the thumb of the post and then accordingly to exclude from the output ...

    If you can help me out I would avoid to slit my wrists ... I'm losing patience behind this thing and I can not figure it out ... : (

    thank you very much,
    Giorgio

Leave a comment

TAG XHTML PERMITS: CODE ENTRY:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL