0

I've added a new column called term_author to the wp_terms table to store which user is inserting each term.

Before a term is inserted, I've added a filter to insert the user ID, but although I'm successfully intercepting before insertion, I'm getting this error upon insert:

PHP Warning:  Attempt to assign property 'term_author' of non-object

My code:

add_filter('wp_insert_term_data', function ($data, $taxonomy, $args) {

    $userId = get_current_user_id();
    $data->{"term_author"} = $userId;
    return $data;

}, 10, 3);

I think I might need to register the new column with the WP engine to allow it to store the user ID, but I'm not sure how.

6
  • I don't think it's a good idea to alter core tables - you could've instead stored the author as a term meta.. And as for the error in question, that's because $data is an array.. – Sally CJ Feb 16 at 0:53
  • 1
    @SallyCJ thank you, I will try storing author as term meta, that makes much more sense – alyx Feb 16 at 0:59
  • @SallyCJ is there a hook to get term ID after insertion so I can store author on term meta? Not sure the best place to do this (I'm very new to WP dev), thank you! – alyx Feb 16 at 1:05
  • 1
    Sorry I had something to do, but yes, that's one of the hook that you can use, but if you want to know whether the term is being created or updated, you'd want to use the saved_<taxonomy> hook instead. – Sally CJ Feb 16 at 1:49
  • 1
    Great thank you, that's even more helpful – alyx Feb 16 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.