Set Primary category using the Yoast SEO plugin

Try instead

update_post_meta($post->ID,'_yoast_wpseo_primary_category',$childid); 

Use below function:

function wpseoPrimaryTerm($taxonomy, $postID, $term){

  if ( class_exists('WPSEO_Primary_Term') ) {
  // Set primary term.
  $primaryTermObject = new WPSEO_Primary_Term($taxonomy, $postID);
  $primaryTermObject->set_primary_term($term);

  // Save primary term.
  $primaryTermObjectAdmin = new WPSEO_Primary_Term_Admin();
  $primaryTermObjectAdmin->save_primary_terms($postID);
  }else{
    echo 'Class WPSEO does not exit';
  }
}

Where $taxonomy - taxonomy name, $PostID - $post->ID, $term - $childid

Leave a Comment