WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Modify date of attachments (5 posts)

  1. herrvollbaer
    Member
    Posted 1 year ago #

    Hi!

    Does anyone know a good way to modify the date of attachments. It's possible by hacking the database or by changing the post type of an attachment to post, but that is not an elegant method.

    I been looking for a plugin that could do the job, but without luck...

    Thanks
    Andreas

  2. s_ha_dum
    Member
    Posted 1 year ago #

    It always helps to know why someone want so do something, so why do you need to do this?

    Not to be a pain... this may be simple or it may be hard. It depends on context.

  3. herrvollbaer
    Member
    Posted 1 year ago #

    I created a simple portfolio theme that displays all image attachments in the database ordered by date (www.andreashackel.de) I want to be able to change the date and therefore the order of the images.

  4. s_ha_dum
    Member
    Posted 1 year ago #

    I think something like this is what you are going to need.

    http://wpengineer.com/2076/add-custom-field-attachment-in-wordpress/

  5. herrvollbaer
    Member
    Posted 1 year ago #

    Hi s_ha_dum!

    Many thanks for the link, it really helped me. To add an edit box that let's you edit the date of an attachment only a few lines of code are needed in the themes function.php:

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    // Set attachment post date:
    
    // Add a custom field to an attachment in WordPress
    function hv_attachment_fields_edit($form_fields, $post) {
        $form_fields['post_date']['label'] = 'Date';
        $form_fields['post_date']['value'] = $post->post_date;
        $form_fields['post_date']['helps'] = 'Set the post date';
        return $form_fields;
    }
    
    // save custom field to post_meta
    function hv_attachment_fields_save($post, $attachment) {
    	$post['post_date'] = $attachment['post_date'];
        return $post;
    }
    
    add_filter( 'attachment_fields_to_edit', 'hv_attachment_fields_edit', 10, 2);
    add_filter( 'attachment_fields_to_save', 'hv_attachment_fields_save', 10, 2);

Topic Closed

This topic has been closed to new replies.

About this Topic