Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

After finding a couple of leads on how to do this, I'm coming up blank. What I am trying to accomplish is to add the post to a new category based on camera name. If the category doesn't exist, then to create it and add.

Essentially what I have below grabs the attachment meta information and saves the camera information into $camera. From there, it kind of falls apart.

function add_post_camera_category() {
global $post;
global $wpdb;
    // Load array with attachment information
    if (is_null($imgID)) {
        $images = get_children(array(
            'post_parent' => $post->ID,
            'post_type' => 'attachment',
            'numberposts' => 1,
            'post_mime_type' => 'image',
            'orderby' => 'ID',
            'order' => 'ASC'
            ));
        if ($images) {
            foreach ($images as $image) {
                $imgID = $image->ID;
            }
        }
    }

    $imgmeta = wp_get_attachment_metadata($imgID);
if ($imgmeta)
{
    if ($imgmeta['image_meta']['camera'])
        $camera = $imgmeta['image_meta']['camera'];

            // Add to custom category
        if(!has_term('','category',$post_ID)){
            $category = get_term_by( 'slug', $camera, 'category' );
            $cat = array($category->slug);
            wp_set_object_terms($post_ID, $cat, 'category');
        }

}

}
add_action('save_post', 'add_post_camera_category');
share|improve this question

2 Answers 2

up vote 1 down vote accepted

First, some comments on your code:

  • the globals $post and $wpdb are not needed, in any case you can declare them as global $post, $wpdb;
  • I don't understand your use of this conditional if (is_null($imgID))
  • you're using get_term_by with "slug", but I guess it's better to do it by "name"

The following is a working code, but I'm not sure if there are any flaws in it. As I was not able to debug the save_post using FirePHP, I did a dirty trick dumping the vars inside a custom field: update_post_meta($post_ID, 'cf_debug_info', print_r($imgmeta,true));.

add_action('save_post', 'wpse_53549_add_post_camera_category', 10, 2);

function wpse_53549_add_post_camera_category($post_ID, $post) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    $images = get_children( array(
        'post_parent' => $post_ID,
        'post_type' => 'attachment',
        'numberposts' => 1,
        'post_mime_type' => 'image',
        'orderby' => 'ID',
        'order' => 'ASC'
    ));

    if ( $images ) 
    {
        $image = array_shift( $images );
        $imgID = $image->ID;
    }

    if ( !$imgID ) 
            return;

    $imgmeta = wp_get_attachment_metadata($imgID);

    if ( $imgmeta )
    {
        // this info may be useful when checking for has_term
        // $defaultcat = get_option('default_category');

        $camera = ($imgmeta['image_meta']['camera']) ? $imgmeta['image_meta']['camera'] : false;        

        if ( $camera )

            // Add to custom category
            if( !has_term( $camera, 'category', $post_ID ) )
            {
                $category = get_term_by( 'name', $camera, 'category' );
                if ( $category )
                {
                    $cat = array( $category->slug );
                    // the last parameter sets if the term must be appended or overwrite the previous
                    wp_set_object_terms( $post_ID, $cat, 'category', true );
                }
                else
                {
                    $id = wp_create_category( $camera );
                    $new_cat = get_term_by( 'id', $id, 'category' );
                    wp_set_object_terms( $post_ID, $new_cat->slug, 'category', true );
                }
            }

    }

}
share|improve this answer
    
This worked brilliantly! –  Pat May 30 '12 at 20:55
    
Bravo! As a photographer I really enjoyed grabbing all those image_meta and seeing the categories magically created with the Camera names (& downloaded samples from different cameras for testing the code:). –  brasofilo May 30 '12 at 21:02
    
Now I just need to figure out how to incorporate it into my theme. So it will say Camera: Canon 5D Mark II, and have that as a link to the Canon 5D2 category. Since the camera meta itself isn't a link, and I want it to link to the correct category using only the camera name, maybe I need to do a fancy category call to strip down the 1 category based on $camera? –  Pat May 30 '12 at 21:42
    
Ain't this just a matter of having a parent category Cameras and using get_categories()‌​?? –  brasofilo May 30 '12 at 22:00
    
Sure, it could be. But on my site, I list out exif information and geotag locations. So rather than blindly listing all categories, I plan on making it "smart" and displaying only the category link for the camera that was used to take the photo displayed. –  Pat May 30 '12 at 22:07

You use $post->ID to get the image data and then you use $post_ID to save your term data. I don't think $post_ID is defined.

I usually take the post ID from the function call Maybe it should be: function add_post_camera_category($post_ID) {

http://codex.wordpress.org/Plugin_API/Action_Reference/save_post

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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