Assign Taxonomy Based on Custom Field Value

admin2025-01-07  3

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?

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?

Share Improve this question edited Aug 7, 2017 at 19:22 Ian S asked Aug 6, 2017 at 15:39 Ian SIan S 316 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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');

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' );
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736259582a600.html

最新回复(0)