wp_insert_post not adding taxonomy (using wp_set_object_terms)

Question

The following runs through a cron job and successfully creates and publishes the post to a custom category.

However, it does not attach a custom taxonomy (ap_job_status) term (ID 3) to the post.

$args = array(
  'post_author'           => $post_author_id,
  'post_date'             => $job['date'],
  'post_content'          => $job['desc'],
  'post_title'            => $job['title'],
  'post_name'             => $job['ref'] . '-' . $job['title'],
  'post_status'           => 'publish',
  'post_type'             => 'ap_job_listings',
  'meta_input'            => array(
    'ap_ref'                => $job['ref'],
    'ap_consultant'         => $job['consultant'],
    'ap_role'               => $job['role'],
    'ap_contract'           => $job['contract'],
    'ap_salary'             => $job['salary'],
    'ap_salary_period'      => $job['period'],
    'ap_skills'             => $job['skills'],
    'ap_location'           => $job['location'],
  ),
);

$post_id = wp_insert_post($args);

$tax     = 'ap_job_status';
$term    = get_term_by( 'id', array(3), $tax);

wp_set_object_terms($post_id, $term, $tax);

I have also (unsuccessfully) tried adding the following to the $args and not using the wp_set_object_terms method…

'tax_input'             => array('ap_job_status' => array(3)),

Some points to note…

1) ‘post_author’ will alway be an administrator

2) Taxonomy in not hierarchical

Code for custom taxonomy is here (just in case its relevant)

add_action( 'init', 'ap_job_status_taxonomy');

function ap_job_status_taxonomy() {  
  register_taxonomy(  
    'ap_job_status',
    'ap_job_listings',
    array(  
      'hierarchical'      => false,  
      'label'             => 'Job Status',
      'show_admin_column' => true,
      'query_var'         => true,
      'exclude_from_search' =>  false,
      'rewrite'           => array(
        'slug'        => 'status',
        'with_front'  => false
      )
    )  
  );  
}  

Thanks in advance for your help!

0
richerimage 1 month 0 Answers 8 views 0

Leave an answer