1

I want to establish a relationship between individual post attachments and Users.
So far I've got my users listing just fine, but it isn't saving/updating.

add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", null, 2);

function my_image_attachment_fields_to_edit($form_fields, $post) {

    $form_fields["belongs_to"]["label"] = 'Belongs to';  
    $form_fields["belongs_to"]["input"] = "html";  
    $form_fields["belongs_to"]["html"] = '';

    $wp_user_query = new WP_User_Query( array( 'orderby' => 'display_name' ) );
    $authors = $wp_user_query->get_results();
if (!empty($authors)) { 
    foreach ($authors as $author) {
        $author_info = get_userdata($author->ID);                       
        $form_fields["belongs_to"]["html"] .= "
        <div style='float:left;width:50%'>
            <input type='checkbox' value='$author_info->ID' name='attachments[{$post->ID}][belongs_to][]' id='belongs_to_user-$author_info->ID' />
            <label for='belongs_to_user-$author_info->ID'>$author_info->first_name $author_info->last_name</label>
        </div>";
    }
}

return $form_fields;
}


add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null, 2);
function my_image_attachment_fields_to_save($post, $attachment) {
if( isset($attachment['my_field']) ){
    // update_post_meta(postID, meta_key, meta_value);
    update_post_meta($post['ID'], 'belongs_to', $attachment['belongs_to']);
}
return $post;
}

I was trying to populate an array with user-IDs, but perhaps that's not the best way to handle it. Any ideas? Thanks in advance!

0

Got it:

add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", null, 2);
function my_image_attachment_fields_to_edit($form_fields, $post) {

    $form_fields["OwnersHeading"]["tr"] = "
    <tr>
        <td colspan='2' style='font-size:16px;padding-left:15px'>Tag Users:</td>
    </tr>";

    // get list of users
    $wp_user_query = new WP_User_Query( array( 'orderby' => 'display_name' ) );
    $authors = $wp_user_query->get_results();

    if (!empty($authors)) { 
        foreach ($authors as $author) {

            $belongs_to_value = (bool)get_post_meta( $post->ID, "_owner_$author->ID", true );

            // create the field
            $form_fields["owner_$author->ID"] = array(
                'input' => 'html',
                'html'  => '
                    <input type="checkbox" id="attachments-' . $post->ID . '-owner'.$author->ID.'" name="attachments[' . $post->ID . '][owner_'.$author->ID.']" value="1"' . ( $belongs_to_value ? ' checked="checked"' : '' ) . ' />
                    <label for="attachments-' . $post->ID . '-owner'.$author->ID.'">'.$author_info->first_name.' '.$author_info->last_name.'</label>',
            );

        }
    }

    return $form_fields;
}


add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null, 2);
function my_image_attachment_fields_to_save($post, $attachment) {

    // get list of users
    $wp_user_query = new WP_User_Query( array( 'orderby' => 'display_name' ) );
    $authors = $wp_user_query->get_results();

    if (!empty($authors)) {
        foreach ($authors as $author) {
            update_post_meta($post['ID'], '_owner_'.$author->ID.'', $attachment['owner_'.$author->ID.'']);      
        }
    }

    return $post;
}

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.