Support

Account

Home Forums General Issues How can I programmatically set a taxonomy field in wp_insert_post?

Solved

How can I programmatically set a taxonomy field in wp_insert_post?

  • Hello!

    I’ve been trying to update the taxonomy field while programatically inserting a post using wp_insert_post() but can’t figure out how to do it. Here is a sample code that is not working – the category_association meta field is empty:

    $cat_id = 100; // Term association that is actually output as a term_id
    
    $post_arr = array(
      'post_title'    => "My title",
      'post_status'   => 'publish',
      'post_type'     => 'page',
      'post_parent'   => $parent_id,
      'post_name'     => "my-slug",
      'meta_input' => array(
        'classified_association' => array($cat_id),
      )
    );
    
    wp_insert_post( $post_arr );
  • …Actually, my code was correct and I had mis-spelled the meta_key name!

    I apologize about this, but I was using trial-and-error for a couple of hours trying different stuff, that should be working.

    Hopefully this thread also helps someone else with similar issues.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.