Programatically Assign Terms From Custom Category Tags

If you need better control over the taxonomy terms (job categories, job types), get categories from custom feed tags, or even set terms on other non-standard taxonomies, you can leverage the plugin available filters.

Since categories behave differently then custom fields, you cannot map them as such. Keep reading.

Let’s consider your feed contains jobs with a tag named ‘<job_category>‘ which contains terms separated by comma (i.e: <job_category>Some Category 1, Some Category 2</job_category>) and you want the importer to assign those those terms as they are imported.

Before continuing, you’ll need to whitelist the custom ‘job_category’ field. To do this, paste the code below on your functions.php or use a plugin like Code Snippets.

Paste this snippet to whitelist the category field:

/*  Whitelist the job category tag */ 
add_filter( 'goft_wpjm_providers_valid_item_tags', 'my_custom_tags' ); 
function my_custom_tags( $fields ) {   
  $fields[] = 'job_category';   
  return $fields; 

Now that the importer recognizes your category field lets give it some instructions to read its terms and assign them to the job category taxonomy called ‘job_listing_category‘:

add_action( 'goft_wpjm_after_insert_job', 'my_custom_tax_input', 10, 3 );
function my_custom_tax_input( $post_id, $item, $params ) {

    if ( ! empty( $item['job_category'] ) ) {

        // Get multiple terms (if any) separated by comma from the feed item
        $job_cats = explode( ',', $item['job_category'] );

        // Sanitize the category names.
        $terms = array_map( 'sanitize_text_field', $job_cats );

        // Set the terms on the 'job_listing_category' taxonomy.
        wp_set_object_terms( $post_id, $terms, 'job_listing_category', false );
    // ... repeat for other taxonomies replacing the tag name and the taxonomy name

This snippet will build a list of terms inside the ‘job_category‘ tag, sanitize them and assign them to ‘job_listing_category‘. Terms that don’t already exist on your job categories will be added.

That’s it! You can now populate job categories from a custom category tag. Complement this tutorial with the custom fields mapping article and you can have any custom data on the feed added to your database.