Created
June 1, 2022 21:44
-
-
Save morgyface/33de5d29fd5dddcc6ae26153c9eb9fcc to your computer and use it in GitHub Desktop.
Revisions
-
morgyface created this gist
Jun 1, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ <?php add_action( 'init', 'import_products', 20 ); function import_products() { $products = array( array( 'title' => 'AFDD060630B', 'description' => 'AFDD RCBO 10A 30mA 1P+N TYPE A', 'term' => 'Switch'), array( 'title' => 'AFDD061030B', 'description' => 'AFDD RCBO 12A 30mA 1P+N TYPE A', 'term' => 'Large Dial'), array( 'title' => 'AFDD061630B', 'description' => 'AFDD RCBO 16A 30mA 1P+N TYPE A', 'term' => 'Rotor') ); $user = get_user_by('login', 'your-username'); $user_id = $user->ID; foreach ( $products as $product ) : $post_title = $product['title']; $prod_description = $product['description']; $term_slug = $product['term']; $term_slug = sanitize_title( $term_slug ); // Converts the term lowercase and replaces spaces with hyphens $post_type = 'products'; $args = array( 'post_author' => $user_id, 'post_type' => $post_type, 'post_title' => $post_title, 'post_status' => 'publish', 'meta_input' => array( 'prod_label' => array( 'value' => 'description', 'label' => 'Description' ), // This is an ACF Select set to Both 'prod_description' => $prod_description ) ); $taxonomy = 'groups'; // The taxonomy slug $term = get_term_by( 'slug', $term_slug, $taxonomy ); $term_id = $term->term_id; // The ID of the taxonomy term if ( $post_object = get_page_by_title( $post_title, OBJECT, $post_type ) ) { $post_id = array( 'ID' => $post_object->ID ); $update = array_merge( $post_id, $args ); wp_set_object_terms( $post_object->ID, array( intval( $term_id ) ), $taxonomy, true ); wp_update_post( $update ); } else { $post_id = wp_insert_post( $args ); wp_set_object_terms( $post_id, array( intval( $term_id ) ), $taxonomy, false ); }; endforeach; }; ?>