ACF: How to update Yoast SEO title and description programmatically on taxonomies

The Yoast SEO plugin for WordPress allows you to optimise your website's taxonomies with custom titles and descriptions. If you want to update this information programmatically, you can use the following function:


function update_taxonomy_seo_title_description( $term_id, $taxonomy, $title, $description ) {
    // Check if the Yoast SEO plugin is active
    if ( class_exists( 'WPSEO_Taxonomy_Meta' ) ) {
        // Update taxonomy title and description
        $yoast_meta = new WPSEO_Taxonomy_Meta( $term_id, $taxonomy );
        $yoast_meta->title = $title;
        $yoast_meta->description = $description;
        $yoast_meta->save();
    }
}

// Usage example: Update Yoast SEO title and description for a taxonomy
$update_term_id = 123; // Replace with the relevant term ID
$update_taxonomy = 'category'; // Replace with the relevant taxonomy
$update_title = 'New Yoast SEO title';
$update_description = 'New Yoast SEO description';

update_taxonomy_seo_title_description( $update_term_id, $update_taxonomy, $update_title, $update_description );

In the above code, we use the function to update the title and description for a specific taxonomy. You need to replace $term_id, $taxonomy, $title and $description with the values that are relevant for your taxonomy.