0

I have added multiple fields to a custom taxonomy. I followed Pippin's tutorial.

This method creates a single option per taxonomy item. The multiple fields are saved in taxonomy_id.

I was not sure if just creating only a single option and having a multi directional array with the taxonomy id in the option would be better.

What are the performance and database implication for the two different methods?

Edit:

The information will normally only be pulled for a single taxonomy.

Here are the two code examples.

/**
 * Save extra taxonomy fields callback function.
 *
 * Single Array
 */
public function save_taxonomy_custom_meta( $term_id, $tt_id ) {
    if ( isset( $_POST['term_meta'] ) ) {

        $term_meta = get_option( 'playlist_' . $term_id );
        $cat_keys = array_keys( $_POST['term_meta'] );

        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = sanitize_text_field( $_POST['term_meta'][$key] );
            }
        }
        // Save the option array.
        update_option(
            'playlist_' . $term_id,
            $term_meta
        );
    }
}

/**
 * Save extra taxonomy fields callback function.
 *
 * Multi-directional array
 */
public function save_taxonomy_custom_meta( $term_id, $tt_id ) {
    if ( isset( $_POST['term_meta'] ) ) {

        $term_meta = get_option( 'playlist_' . $term_id );
        $cat_keys = array_keys( $_POST['term_meta'] );

        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = sanitize_text_field( $_POST['term_meta'][$key] );
            }
        }
        //
        $term_meta[$term_id] = $term_meta;
        // Save the option array.
        update_option(
            'plugin_name_playlist',
            $term_meta
        );
    }
}
  • Having difficulty understanding exactly what you are setting up. Can you flesh out an example of the taxonomy and the info stored? – jdm2112 Aug 3 '14 at 14:35
  • @jdm2112 I have added a code example for both methods when saving the options. – grappler Aug 3 '14 at 18:52
0

If the option is auto loading (and in pippin's code they are) then there isn't any difference except for coding style as all auto loadable options are fetched together in one query in init time and cached in the memory and at this point get_option is not more then a wrapper around accessing a php array.

If you decide to use not auto loading option it becomes a question of how are you going to use the data, if mostly to get the info for a single term then having a multi dimensional array that will bring info about other terms is a waste.

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.