1

I have a custom post type "vegetables"

I have a series of custom fields for each month of the year as check boxes

I have a custom taxonomy "Seasons" that I want to assign based on what boxes are checked in my custom fields

Summer if any June July August

Fall if any September October November

Winter if any December January February

Spring if any March April May

So the vegitable could be assigned all season or a single season based on what custom fields are checked

add_action( 'save_post', 'assign_cat_to', 10, 1 );

function assign_cat_to( $post_id ) {
if ( 'seasonal-product' !== get_post_type( $post_id ) ) {
    return;
}

$term_slugs = array();

foreach ( $_POST as $field => $value ) {
    if ( 0 === strpos( $field, 'wpcf-available-in-' ) && '1' === $value ) {
        $term_slugs[] = str_replace( 'wpcf-available-in-', '', $field );
    }
}

if ( empty( $term_slugs ) ) {
    return;
}

$term_ids = array();

foreach ( $term_slugs as $term_slug ) {
    $term       = get_term_by( 'slug', $term_slug, 'season' );
    $term_ids[] = $term->term_id;
}

wp_set_object_terms( $post_id, $term_ids, 'season' );
}

I have made some updates based on actual fields and terms but I still do not understand how to assign "spring, summer, fall, winter" to the term.

Alternatively I could store the season name "spring, summer, fall, winter" to the database instead of "1" would that make it easier to write to the term slug?

2 Answers 2

0
/**
 * Programmatically assign taxonomy term by custom fields
 * 
 * @param int $post_id
 */
function rd_assign_taxonomies($post_id) {

    if (get_post_type($post_id) == 'vegetables') {
        $terms = array();

        // Here we'd check to see if the post has the specific fields, and if so add the IDs or slugs of the taxonomy terms to the $terms array
        // The exact details will depend on how the checkboxes were implemented, for example as native postmeta or ACF fields

        wp_set_object_terms( $post_id, $terms, 'seasons', false );
    }
}

add_action('save_post', 'rd_assign_taxonomies');
1
  • Does this also work for assigning a category to a product using a custom field (woocommerce)?
    – Amjad
    Commented Mar 18, 2019 at 1:40
0

What about assigning programatically the term and taxonomy to your custom post with wp_set_object_terms ?

add_action( 'save_post', 'assign_cat_to', 10, 1 );

function assign_cat_to( $post_id ) {
    if ( 'vegetables' !== get_post_type( $post_id ) ) {
        return;
    }

    $term_slugs = array();

    foreach ( $_POST as $field => $value ) {
        if ( 0 === strpos( $field, 'available_in_' ) && '1' === $value ) {
            $term_slugs[] = str_replace( 'available_in_', '', $field );
        }
    }

    if ( empty( $term_slugs ) ) {
        return;
    }

    $term_ids = array();

    foreach ( $term_slugs as $term_slug ) {
        $term       = get_term_by( 'slug', $term_slug, 'seasons' );
        $term_ids[] = $term->term_id;
    }

    wp_set_object_terms( $post_id, $term_ids, 'seasons' );
}
5
  • 1
    Surely you wouldn't need to check capabilities as they're already checked by core before the save_post action fires. Other than that, great minds etc.
    – Chris Cox
    Commented Aug 6, 2017 at 16:45
  • OK I get the bit at the top and bottom. In my case I have 12 separate fields. Such as "available-in-january" that save a value of 1 to the database if true and 0 to the database if false. So I would need to test if any of three months were true and then assign a Season, also some items will need to be assigned multiple seasons. Just not sure how to code this up. Thanks for the help!
    – Ian S
    Commented Aug 7, 2017 at 15:21
  • @IanS, please, see my updated answer and let me know how it's working...
    – ClemC
    Commented Aug 7, 2017 at 16:14
  • @IanS, note, I assume you follow this naming convention for all your custom field IDs/names: available_in_{month}... You can replace the underscores with commas but I prefer to let it this way since WP convention is underscore... Note also that {month} should be the slug of your term.
    – ClemC
    Commented Aug 7, 2017 at 16:34
  • Does this still work?
    – Amjad
    Commented Mar 18, 2019 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.