How to set Job Category from another custom Taxonomy when using WP Job Manager Field Editor

functions.php

<?php
add_action( 'job_manager_field_editor_save_custom_field_end', 'update_my_custom_tax_field_editor_fields', 10, 5 );

/**
 * Update Listing job_category from job_category_dentist taxonomies
 * 
 * REQUIRES: WP Job Manager Field Editor 1.4.2+
 * 
 * This can be useful if you want to set the Job Category but for some reason have that field hidden,
 * disabled, or set to not show for a specific package, but still want the category set (or maybe you want
 * to use a custom taxonomy with less options).
 *
 * @param string  $custom_field        Meta key without prepended underscore
 * @param integer $job_id              Listing ID
 * @param array   $custom_field_config Custom field configuration array
 * @param array   $values              All field values
 * @param string  $type                Type of fields (job, resume, etc)
 */
function update_my_custom_tax_field_editor_fields( $meta_key, $job_id, $mk_conf, $values, $type ){

    // We're only looking to update on the job_category_dentist metakey, for job fields
    if( $meta_key != 'job_category_dentist' || $type != 'job' ) {
        return;
    }

    // Make sure taxonomy config is available, and a value is set for this specific meta key
    if( empty( $mk_conf['taxonomy'] ) || ! isset( $values[ $type ][ $meta_key ]) ) {
        return;
    }

    // Make sure dentist_categories is an array of values
    $dentist_categories = is_array( $values[ $type ][ $meta_key ] ) ? $values[ $type ][ $meta_key ] : array( $values[ $type ][ $meta_key ] );

    $term_slugs = array();

    foreach( $dentist_categories as $cat_id ){
        $term = get_term( $cat_id, $mk_conf['taxonomy'] );
        if( is_wp_error( $term ) || ! is_object( $term ) ) continue;

        $term_slugs[] = $term->slug;
    }

    $result = wp_set_object_terms( $job_id, $term_slugs, 'job_listing_category', FALSE );
}
No comments yet.

Leave a Reply