0

I'm using the IMDB Connecter plugin to grab meta data for movies and I'm wondering if I can save some of the information it generates as terms in a custom taxonomy that already exists.

What I want to do is to be able to save the "Director" and "Actors" information generated by the plugin as taxonomy terms. So for example, the values in the array generated by the $actors function would be saved as individual terms in my custom taxonomy actors.

This is my current code.

$imdb = get_field('imdb_id'); // IMDB movie ID is saved as a custom meta value
$imdbInfo = get_imdb_connector_movie($imdb);
$actors = $imdbInfo['actors']; // Gets an array of actor names
$director = $imdbInfo['director']; // Gets an array of directors

Is it possible to save these values as custom terms?

1

It's as easy as:

function wpse_199523_save_imdb_terms( $post_id ) {
    if ( ! $imdb = get_field( 'imdb_id', $post_id ) )
        return;
    if ( ! $data = imdb_connector_get_movie( $imdb ) )
        return;

    if ( ! empty( $data['actors'] ) )
        wp_set_object_terms( $post_id, $data['actors'], 'actors' );
    if ( ! empty( $data['director'] ) )
        wp_set_object_terms( $post_id, $data['director'], 'directors' );
}

add_action( 'wp_insert_post', 'wpse_199523_save_imdb_terms', 500 /* Ensure we run after ACF */ );

Refer to the codex on wp_set_object_terms

  • This didn't work for me. It didn't create the new taxonomy terms – Dean Elliott Aug 25 '15 at 13:50
  • Typo, see edited answer. – TheDeadMedic Aug 25 '15 at 14:27
  • still nothing. I tried it like this and also by changing get_imdb_connector_movie to imdb_connector_get_movie like it is in the plugin and neither worked. – Dean Elliott Aug 25 '15 at 14:32
  • Make sure you have WP_DEBUG defined as true in your wp-config.php, add exit; inside the end of the function, then re-save the post and see what errors (if any) you get. – TheDeadMedic Aug 25 '15 at 14:48
  • nothing came up. No errors. – Dean Elliott Aug 25 '15 at 14:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.