Taxonomies in attachments

Home Forums TurboCSV Support Taxonomies in attachments

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #10347
    shawkins
    Participant

    I have a plugin that facilitates adding and displaying tags and custom taxonomy values for attachments.  It works fine but adds values attachment by attachment. Much too slow!

    I would like to use TurboCSV to do bulk loads but it doesn’t seem to work. It appears to populate wp_terms and wp_terms_ taxonomy properly but it does not add records to wp_term_relationships.

    Any suggestions?

    Thanks,

    Stephen

    #10349
    shawkins
    Participant

    OK, I think I understand what is happening. TurboCSV uses wp_insert_post and wp-update_posts to insert and update records.  These in turn distinguish between attachments and all other post types. For attachments they call wp_insert_attachment and wp_update_attachment which to do not process taxonomies.

    To link attachments to taxonomies, I think that you would need to use something like wp_set_object_terms. The logic would seem to be simple. Once the normal insert/update is done, if the post_type is attachment and there are taxonomy fields to be added/updated call wp_set_object terms.

    If this makes sense, could you let me know where to put this and and how to extract the tags and taxonomy parameters from the TurboCSV tables?

    Thanks,

    Stephen

    #10354
    shawkins
    Participant

    Hi Chris,

    The code below inserted after the update/insert has been executed around line 280 in ti_import.php seems to work.
        //Special treatment for taxonomies associated with attachments
            $tax_input = $post[‘tax_input’];
    $append = true; // append or replace taxonomy values

            if ( !empty($tax_input) && get_post_type($post_id)==’attachment’ ) {
                foreach ( $tax_input as $taxonomy => $tags ) {
                    $taxonomy_obj = get_taxonomy($taxonomy);
                    if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.
                        $tags = array_filter($tags);
                    if ( current_user_can($taxonomy_obj->cap->assign_terms) )
                        wp_set_post_terms( $post_id, $tags, $taxonomy, $append );
                }
            }
    Incidentally, you might want to consider treating all taxonomy inserts/updates like this. wp_insert_post  does a full replace of taxonomy values. With wp_set_post_terms you can either replace or append values.  If you have the option of appending values, you can build up taxonomy structures incrementally. I expect that this would be a common use case when doing bulk loads.

    I have also come to appreciate the elegance of your coding. It makes trouble shooting so much easier!

    Regards,

    Stephen

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.