Support

Account

Forum Replies Created

  • Yeah, I wish the database update built into ACF removed the options data. I had to do it manually, but it was easy, only because I prefix all my ACF fields, so I was able to write a query for that prefix.

    You could create a PHP script to loop through your term_meta ACF fields, that is about the only other way I can think to get them in a non-manual manner.

  • You can, but you should backup your DB, and also confirm that they are in the term_meta table. I was able to clear our my options tables without issue, after the ACF update ran.

  • Yes, @tdmalone, I have actually ran into a number of cases where I have had to update my code. It is actually a complex problem to solve.

    I would really like to know the answer to: Is ACF going to support termmeta or not?

    Because right now I am just going under the assumption they will soon, so I am only investing time in solving code issues as they arise in my use cases. Rather than trying to go bulletproof.

    For the sake of ACF, I hope they support it soon, It is essential to have termmeta in the wp_termmeta table if you want to properly running a meta_query based on meta of you taxonomies.

  • @truheart

    @edir

    Here is an even better way. It clears out the options table. so you aren’t double saving values. And if ACF ever decides to start using the termmeta table you will be all set and can just remove these functions.

    You would want to modify the “if ( $field_group[‘location’][0][0][‘param’] == ‘taxonomy’ )” line if you were using complex location rules.

    
    add_action( 'acf/init', 'prefix_acf_init' );
    /**
     * Prepare for any crazy stuff we are about to do
     */
    function prefix_acf_init() {
    	global $prefix_acf_delete_values;
    
    	$prefix_acf_delete_values = [ ];
    }
    
    add_action( 'acf/save_post', 'prefix_after_save_post', 100001 );
    /**
     * Cleanup after everything is done
     */
    function prefix_after_save_post() {
    	global $prefix_acf_delete_values;
    
    	if ( ! empty( $prefix_acf_delete_values ) ) {
    		foreach ( $prefix_acf_delete_values as $delete ) {
    			acf_delete_value( $delete['post_id'], $delete['field'] );
    		}
    	}
    }
    
    add_filter( 'acf/update_value', 'prefix_update_term_meta', 10, 3 );
    /**
     * Update term meta based on ACF term meta fields
     *
     * @param $value
     * @param $post_id
     * @param $field
     *
     * @return
     */
    function prefix_update_term_meta( $value, $post_id, $field ) {
    	global $prefix_acf_delete_values;
    
    	$field_group = acf_get_field_group( $field['parent'] );
    
    	if ( $field_group['location'][0][0]['param'] == 'taxonomy' ) {
    		$term_id = intval( filter_var( $post_id, FILTER_SANITIZE_NUMBER_INT ) );
    
    		if ( $term_id > 0 ) {
    			$update_term_meta = update_term_meta( $term_id, $field['name'], $value );
    
    			if ( ! is_wp_error( $update_term_meta ) )
    				$prefix_acf_delete_values[ $field['key'] ] = [ 'post_id' => $post_id, 'field' => $field ];
    		}
    	}
    
    	return $value;
    }
    
    add_filter( 'acf/load_value', 'prefix_load_term_meta', 10, 3 );
    /**
     * Load term meta in for ACF meta fields
     *
     * @param $value
     * @param $post_id
     * @param $field
     *
     * @return mixed
     */
    function prefix_load_term_meta( $value, $post_id, $field ) {
    	$field_group = acf_get_field_group( $field['parent'] );
    
    	if (
    		$field_group['location'][0][0]['param'] == 'taxonomy' ||
    		(
    			! in_array( $post_id, [ 'option', 'options' ] ) &&
    			! $field_group &&
    			strpos( $post_id, '_' ) !== false
    		)
    	) {
    		$term_id = intval( filter_var( $post_id, FILTER_SANITIZE_NUMBER_INT ) );
    
    		if ( $term_id > 0 ) {
    			$value = get_term_meta( $term_id, $field['name'], true );
    		}
    	}
    
    	return $value;
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)