0

I've got the following example code, which creates a custom field and adds to the attachment meta:

function be_attachment_field_credit( $form_fields, $post ) {
    $form_fields['be-photographer-name'] = array(
        'label' => 'Photographer Name',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'be_photographer_name', true ),
        'helps' => 'If provided, photo credit will be displayed',
    );

    $form_fields['be-photographer-url'] = array(
        'label' => 'Photographer URL',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'be_photographer_url', true ),
        'helps' => 'Add Photographer URL',
    );

    return $form_fields;
}

add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );


function be_attachment_field_credit_save( $post, $attachment ) {
    if( isset( $attachment['be-photographer-name'] ) )
        update_post_meta( $post['ID'], 'be_photographer_name', $attachment['be-photographer-name'] );

    if( isset( $attachment['be-photographer-url'] ) )
update_post_meta( $post['ID'], 'be_photographer_url', esc_url( $attachment['be-photographer-url'] ) );

    return $post;
}

add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 );

I've tried calling var_dump(get_post_meta($post->ID));, but I assume that meta is somewhere else, as whatever input is not there.

According to the codex, I can call:

wp_get_attachment_metadata( $attachment_id, $unfiltered );.

However, I have no idea how to get the $attachment_id.

My eventual goal is to add this meta data to the Advanced Custom Fields Gallery plugin, which adds the attachment meta to the post meta (I believe) — but that's down the road.

  • Where are you trying to get the attachement ID? The attachement ID is not meta data. – cybmeta Mar 26 '14 at 10:00
  • I'm trying to get whatever data has been submitted in the above code, and then add it to the post meta. So I can retrieve it like so: get_post_meta($post->ID) – tmyie Mar 26 '14 at 10:34
1

"Attachments" aren't meta data. That is why you can't retrieve them with get_post_meta(). "Attachments" are a post type that is attached to the parent post. You need to pull the attachment posts that have you post set as parent, and then use those IDs to get the attachment meta.

$args = array(
    'post_parent' => $post->ID,
    'post_type'   => 'attachment', 
    'numberposts' => -1 
);
$children = get_children($args);

foreach ($children as $c) {
  $att = wp_get_attachment_metadata( $c->ID, $unfiltered );
  var_dump($att);
}
| improve this answer | |
0

All the post meta you're saving is being attached to a post of type attachment, so in this case $post->ID is the attachment ID.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.