<?php /** * Add Photographer selection to media upload * * @param $form_fields array, fields to include in attachment form * @param $post object, attachment record in database * @return $form_fields, modified form fields */ function bsj_attachment_field_credit( $form_fields, $post ) { //do_dump($post); $form_fields['jdp_photographer'] = array( 'label' => 'Contributor:', 'input' => 'html', 'helps' => 'Choose the photographer/artist for this image', ); //GET THE CURRENT PHOTOGRAPHER $current_photog = get_post_meta($post->ID, 'jdp_photographer', true); //do_dump($current_photog); if ( !isset($current_photog) ) { $current_photog = 0; } //SETUP THE FORM FIELD $form_fields['jdp_photographer']['html'] = '<select name="attachments['.$post->ID.'][jdp_photographer]" id="attachments['.$post->ID.'][jdp_photographer]">'; //OUTPUT NULL FIELD $form_fields['jdp_photographer']['html'] .= '<option value="0" '.selected(0, $current_photog, false).'>Choose one...</option>'; //GET THE TERMS IN THE 'jdp_contributor_types' taxonomy, excluding authors $terms = get_terms( 'jdp_contributor_types', array('fields' => 'ids', 'hide_empty' => false, 'exclude' => '32') ); //GET THE PHOTOGRAPHERS $args = array( 'post_type' => 'jdp_contributors', 'nopaging' => true, 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'jdp_contributor_types', 'field' => 'id', 'terms' => $terms ), ), 'orderby' => 'name', 'order' => 'ASC', ); $photographers = new WP_QUERY($args); do_dump($photographers); //LOOP THRU THE PHOTOGRAPHERS foreach( $photographers as $postdata ) { setup_postdata($postdata); $form_fields['jdp_photographer']['html'] .= '<option value="'.$postdata->ID.'" '.selected($postdata->ID, $current_photog, false).'>'.$postdata->post_title.'</option>'; } wp_reset_query(); $form_fields['jdp_photographer']['html'] .= '</select>'; return $form_fields; } add_filter( 'attachment_fields_to_edit', 'bsj_attachment_field_credit', 10, 2 ); /** * Save values of Photographer Name and URL in media uploader * * @param $post array, the post data for database * @param $attachment array, attachment fields from $_POST form * @return $post array, modified post data */ function jdp_attachment_field_credit_save( $post, $attachment ) { if (isset( $attachment['jdp_photographer'] ) ) update_post_meta( $post['ID'], 'jdp_photographer', $attachment['jdp_photographer'] ); return $post; } add_filter( 'attachment_fields_to_save', 'jdp_attachment_field_credit_save', 10, 2 ); ?>