Created
May 27, 2018 17:24
-
-
Save mtx-z/784ecd7b9941253ef3c9e0e19bf0a6a7 to your computer and use it in GitHub Desktop.
WordPress\ACF - set real term_meta on ACF terms custom fields on update/create
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ACF fix for term meta | |
* replace {{custom_term_meta_x}} by your ACF term custom field name | |
* TODO: loop over all term_meta to auto-generate filters | |
* | |
* https://support.advancedcustomfields.com/forums/topic/how-to-use-wp-term-meta-on-acf-the-easy-way/ | |
*/ | |
function acf_update_term_meta( $value, $post_id, $field ) { | |
$term_id = (int) filter_var( $post_id, FILTER_SANITIZE_NUMBER_INT ); | |
if ( $term_id > 0 ) { | |
update_term_meta( $term_id, $field['name'], $value ); | |
} | |
return $value; | |
} | |
add_filter( 'acf/update_value/name={{custom_term_meta_1}}', 'acf_update_term_meta', 10, 3 ); | |
add_filter( 'acf/update_value/name={{custom_term_meta_2}}', 'acf_update_term_meta', 10, 3 ); | |
function acf_load_term_meta( $value, $post_id, $field ) { | |
$term_id = (int) filter_var( $post_id, FILTER_SANITIZE_NUMBER_INT ); | |
if ( $term_id > 0 ) { | |
$value = get_term_meta( $term_id, $field['name'], true ); | |
} | |
return $value; | |
} | |
add_filter( 'acf/load_value/name={{custom_term_meta_1}}', 'acf_load_term_meta', 10, 3 ); | |
add_filter( 'acf/load_value/name={{custom_term_meta_2}}', 'acf_load_term_meta', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment