Home Forums General Issues Set Default Taxonomy


Set Default Taxonomy

  • Hi All,

    I’m hoping someone can help!?

    I’ve got an ACF Taxonomy called Custom Stock Status (custom_stock_status)

    It pulls in from a WooCommerce Attribute (taxonomy) called Custom Stock Status (pa_custom-stock-status)

    It has a value of Available to Order ( slug available-to-order )

    My understanding is I can set that value doing something like:
    $term_taxonomy_ids = wp_set_object_terms( $post->ID, 'available-to-order', 'custom_stock_status');
    However, I can’t seem to get it to work.

    The idea being when a products created, I can use the following code to preset the attribute:

    add_action('save_post_product', 'initiate_product', 10, 3);
    function initiate_product($postID, $post, $update) {
        if (!$update) {
            // $update is false if we're creating a new post
    	   $term_taxonomy_ids = wp_set_object_terms( $post->ID, 'available-to-order', 'custom_stock_status');	

    Yet it doesn’t work. I’m clearly missing something obvious, so would be grateful of any pointers!

    Many thanks

  • What you’re doing will only set the terms when the post is saved and you won’t see it until the next time the edit page is loaded. These things happen after the post is saved.

    In order to preset a taxonomy field with a term you need to use an acf/prepare_field filter and set $field['default_value'] to the term you want, or an array of terms if the field allows multiple selections.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.