• Resolved kamyarhu

    (@kamyarhu)


    I have assigned an ACF image field to the taxonomy event-category. When the taxonomy term is edited and saved, I would like to replace the ‘social image’ field from TSF with the ACF image field.

    function filter_wp_insert_term_data( $data, $taxonomy, $args ) { 
    	$data['social_image_url'] = wp_get_attachment_url($data['featured_image']);
        return $data;
    }; 
    add_filter( 'wp_insert_term_data', 'filter_wp_insert_term_data', 10, 3 ); 

    I guess for terms TSF fields are not stored as separate meta values in db.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello!

    You guessed correctly: TSF uses a key-value store for term metadata.

    You can update a single value using the_seo_framework()->update_single_term_meta_item(). To prevent infinite loops whilst saving via wp_insert_term_data, I recommend using action edit_term at priority higher than 10.

    add_action(
    	'edit_term',
    	static function( $term_id, $tt_id, $taxonomy ) {
    
    		if ( ! function_exists( 'the_seo_framework' ) ) return;
    
    		$featured_id = get_term_meta( $term_id, 'featured_image', true ) ?: 0;
    
    		if ( ! $featured_id ) return;
    
    		$tsf = the_seo_framework();
    
    		$tsf->update_single_term_meta_item(
    			'social_image_url',
    			wp_get_attachment_url( $featured_id ),
    			$term_id,
    			$tt_id,
    			$taxonomy
    		);
    
    		// Also store the ID to help generate image size metadata.
    		$tsf->update_single_term_meta_item(
    			'social_image_id',
    			$featured_id,
    			$term_id,
    			$tt_id,
    			$taxonomy
    		);
    	},
    	100,
    	3
    );

    I think that will do. I didn’t test it, however.

    Thread Starter kamyarhu

    (@kamyarhu)

    Thank you so much, it works like a charm!

    Thread Starter kamyarhu

    (@kamyarhu)

    I will appreciate one more addition, to check if ‘social_image_url’ is empty before updating it. To update only if it is empty. I tried with $tsf->get_term_meta_item[‘social_image_url’] but couldn’t make it work.

    • This reply was modified 3 years, 5 months ago by kamyarhu.
    Plugin Author Sybre Waaijer

    (@cybr)

    Hi again!

    Sorry for my negligence. Do you still require support with this?

    If you wish to fetch a term-meta-item quickly after it’s changed, you’d want to set $use_cache to false; this way, it’ll always try to fetch the latest data. E.g.:

    $url = $tsf->get_term_meta_item( 'social_image_url', $term_id, false );
    
    Thread Starter kamyarhu

    (@kamyarhu)

    Thank you, this is very helpful

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Taxonomy Term: use ACF image field for social image’ is closed to new replies.