最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Woocommerce product not appearing in category list page when created programatically

matteradmin8PV0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I am creating a plugin that will filter the products from API to our WordPress site. I did successfully created the product and automatically publish it with category and subcategory in it.

The problem is when I view it on my category pages it won't show up. I need to manually click the update or publish button in order for the products to show up.

What would be the issue on this here is the code below:

$post_id = wp_insert_post( array(
                'post_title' => $items['description'],
                'post_content' => $items['detail'],
                'post_excerpt' => $items['description_2'],
                'post_status' => 'publish',
                'post_type' => "product",
            ) );

            // Set Product type
            // wp_set_object_terms( $post_id, 'simple', 'product_type');                            
            wp_set_post_terms( $post_id, 'simple', 'product_type');                         
            // Get parent category returns mixed or objects
            $category_parent = get_term_by('name', 'Lense', 'product_cat');                     
            // Get child category returns array
            $category_child = term_exists( 'Canon', 'product_cat', $category_parent->term_id);

            // Set product brand
            // wp_set_object_terms( $post_id, 'Canon', 'product_brand');
            // wp_set_post_terms( $post_id, 'Canon', 'product_brand');

            // Set category for this product post
            // wp_set_object_terms( $post_id, $category_parent->term_id, 'product_cat', false); 
            // wp_set_object_terms( $post_id, (int)$category_child['term_id'], 'product_cat', true);

            wp_set_post_terms($post_id, array($category_parent->term_id,(int)$category_child['term_id']), 'product_cat');


            // add_post_meta( $post_id, '_wc_booking_availability', $availability );

            update_post_meta( $post_id, '_visibility', 'visible' );
            update_post_meta( $post_id, '_booking_option', 'yes' );
            update_post_meta( $post_id, '_stock_status', 'instock');
            // update_post_meta( $post_id, 'total_sales', '0' );
            // update_post_meta( $post_id, '_downloadable', 'no' );
            // update_post_meta( $post_id, '_virtual', 'yes' );
            update_post_meta( $post_id, '_regular_price', 0);
            update_post_meta( $post_id, '_sale_price', 0);
            // update_post_meta( $post_id, '_purchase_note', '' );
            // update_post_meta( $post_id, '_featured', 'no' );
            // update_post_meta( $post_id, '_weight', '' );
            // update_post_meta( $post_id, '_length', '' );
            // update_post_meta( $post_id, '_width', '' );
            // update_post_meta( $post_id, '_height', '' );
            // update_post_meta( $post_id, '_sku', $items['lookup_code'] );
            // update_post_meta( $post_id, '_product_attributes', array() );
            // update_post_meta( $post_id, '_sale_price_dates_from', '' );
            // update_post_meta( $post_id, '_sale_price_dates_to', '' );
            update_post_meta( $post_id, '_price', $items['default_price']);
            // update_post_meta( $post_id, '_sold_individually', '' );
            // update_post_meta( $post_id, '_manage_stock', 'no' );
            // update_post_meta( $post_id, '_backorders', 'no' );
            // update_post_meta( $post_id, '_stock', '' );
            // update_post_meta( $post_id, '_', '' );


            // ===================== Create product image =================
            $image = $items['cover_img_url'];

            // magic sideload image returns an HTML image, not an ID
            $media = media_sideload_image($image, $post_id);

            // therefore we must find it so we can set it as featured ID
            if(!empty($media) && !is_wp_error($media)){
                $args = array(
                    'post_type' => 'attachment',
                    'posts_per_page' => -1,
                    'post_status' => 'any',
                    'post_parent' => $post_id
                );

                // reference new image to set as featured
                $attachments = get_posts($args);

                if(isset($attachments) && is_array($attachments)){
                    foreach($attachments as $attachment){
                        // grab source of full size images (so no 300x150 nonsense in path)
                        $image = wp_get_attachment_image_src($attachment->ID, 'full');
                        // determine if in the $media image we created, the string of the URL exists
                        if(strpos($media, $image[0]) !== false){
                            // if so, we found our image. set it as thumbnail
                            set_post_thumbnail($post_id, $attachment->ID);
                            // only want one image
                            break;
                        }
                    }
                }
            }


            // ================= Create Product Gallery
            $images = $items['images'];
            if(isset($images) && is_array($images)){

                $list_id = "";

                // magic sideload image returns an HTML image, not an ID
                $media = media_sideload_image($images[1]['full_url'], $post_id);

                // therefore we must find it so we can set it as featured ID
                if(!empty($media) && !is_wp_error($media)){
                    $args = array(
                        'post_type' => 'attachment',
                        'posts_per_page' => -1,
                        'post_status' => 'any',
                        'post_parent' => $post_id
                    );

                    // reference new image to set as featured
                    $attachments = get_posts($args);

                    if(isset($attachments) && is_array($attachments)){
                        foreach($attachments as $attachment){
                            // grab source of full size images (so no 300x150 nonsense in path)
                            $image = wp_get_attachment_image_src($attachment->ID, 'full');
                            // determine if in the $media image we created, the string of the URL exists
                            if(strpos($media, $image[0]) !== false){
                                // if so, we found our image. set it as thumbnail
                                // set_post_thumbnail($post_id, $attachment->ID);
                                $list_id .= $attachment->ID . ",";
                                // only want one image
                                break;
                            }
                        }
                    }
                }

                update_post_meta($post_id,'_product_image_gallery',$list_id);

Archive-Product.php code

<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see /
 * @package WooCommerce/Templates
 * @version 3.4.0
 */

defined( 'ABSPATH' ) || exit;

get_header( 'shop' );


/**
 * Hook: woocommerce_before_main_content.
 *
 * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
 * @hooked woocommerce_breadcrumb - 20
 * @hooked WC_Structured_Data::generate_website_data() - 30
 */
do_action( 'woocommerce_before_main_content' );

?>
<div class="site-main-product">
<header class="woocommerce-products-header">
    <?php /*if ( apply_filters( 'woocommerce_show_page_title', true ) ) :*/ ?>
        <!-- <h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1> -->
    <?php /*endif;*/ ?>

    <?php
    /**
     * Hook: woocommerce_archive_description.
     *
     * @hooked woocommerce_taxonomy_archive_description - 10
     * @hooked woocommerce_product_archive_description - 10
     */
    /*do_action( 'woocommerce_archive_description' );*/
    ?>

</header>
<?php
if ( woocommerce_product_loop() ) {

    /**
     * Hook: woocommerce_before_shop_loop.
     *
     * @hooked wc_print_notices - 10
     * @hooked woocommerce_result_count - 20
     * @hooked woocommerce_catalog_ordering - 30
     */
    /*do_action( 'woocommerce_before_shop_loop' );*/
    ?>

    <div class="shop-page-notices">
        <div class="woocommerce-notices-shortcode woocommerce">
            <?php wc_print_notices(); ?>
        </div>
    </div>

    <?php if(is_shop()) : ?>
        <div class="shop-title">
            Best Rental Item
        </div>

    <?php else : ?>
        <div class="shop-title">
            <?php echo apply_filters( 'woocommerce_categories_header_title', wp_title( '»', false ) ); ?>
        </div>
    <?php endif; ?> 

    <?php

    if ( wc_get_loop_prop( 'total' ) ) {

        $taxonomy = 'product_cat';
        $taxonomy_terms = get_terms( $taxonomy );

        foreach ($taxonomy_terms as $key => $value) {
            $is_displayed = false;
            while ( have_posts() ) {
                the_post();

                $brands = get_the_terms(get_the_ID(), $taxonomy);

                if(!empty($brands)){
                    foreach ($brands as $key => $brand) {
                        if($brand->term_taxonomy_id==$value->term_id){
                            $is_displayed = true;
                            break;
                        }
                    }

                }
            }
            if($is_displayed==true){
                $is_displayed = false;
            ?>  

            <div class="shop-archive-brand-title">
                <img src="<?php echo $imageURL = wp_get_attachment_image_url(get_term_meta($value->term_id, 'thumbnail_id', true), 'medium'); ?>" style="height: 60px;"/>
                <div class="shop-archive-brand-title-middle-line">
                    <hr>
                </div>
            </div>

            <?php

                woocommerce_product_loop_start();
                while ( have_posts() ) {
                    the_post();

                    $brands = get_the_terms(get_the_ID(), $taxonomy);

                    if(!empty($brands)){
                        foreach ($brands as $key => $brand) {
                                        //var_dump($value);
                            if($brand->term_taxonomy_id==$value->term_id){
                                do_action( 'woocommerce_shop_loop' );
                                wc_get_template_part( 'content', 'product' );
                            }
                        }
                    }
                }
            }
            woocommerce_product_loop_end();
        }
    }



    /**
     * Hook: woocommerce_after_shop_loop.
     *
     * @hooked woocommerce_pagination - 10
     */
    do_action( 'woocommerce_after_shop_loop' );
} else {
    /**
     * Hook: woocommerce_no_products_found.
     *
     * @hooked wc_no_products_found - 10
     */
    do_action( 'woocommerce_no_products_found' );
}

/**
 * Hook: woocommerce_after_main_content.
 *
 * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
 */
do_action( 'woocommerce_after_main_content' );

/**
 * Hook: woocommerce_sidebar.
 *
 * @hooked woocommerce_get_sidebar - 10
 */
?>
</div>

<?php
do_action( 'woocommerce_sidebar' );

get_footer( 'shop' );
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I am creating a plugin that will filter the products from API to our WordPress site. I did successfully created the product and automatically publish it with category and subcategory in it.

http://prntscr/mdl1jb

The problem is when I view it on my category pages it won't show up. I need to manually click the update or publish button in order for the products to show up.

What would be the issue on this here is the code below:

$post_id = wp_insert_post( array(
                'post_title' => $items['description'],
                'post_content' => $items['detail'],
                'post_excerpt' => $items['description_2'],
                'post_status' => 'publish',
                'post_type' => "product",
            ) );

            // Set Product type
            // wp_set_object_terms( $post_id, 'simple', 'product_type');                            
            wp_set_post_terms( $post_id, 'simple', 'product_type');                         
            // Get parent category returns mixed or objects
            $category_parent = get_term_by('name', 'Lense', 'product_cat');                     
            // Get child category returns array
            $category_child = term_exists( 'Canon', 'product_cat', $category_parent->term_id);

            // Set product brand
            // wp_set_object_terms( $post_id, 'Canon', 'product_brand');
            // wp_set_post_terms( $post_id, 'Canon', 'product_brand');

            // Set category for this product post
            // wp_set_object_terms( $post_id, $category_parent->term_id, 'product_cat', false); 
            // wp_set_object_terms( $post_id, (int)$category_child['term_id'], 'product_cat', true);

            wp_set_post_terms($post_id, array($category_parent->term_id,(int)$category_child['term_id']), 'product_cat');


            // add_post_meta( $post_id, '_wc_booking_availability', $availability );

            update_post_meta( $post_id, '_visibility', 'visible' );
            update_post_meta( $post_id, '_booking_option', 'yes' );
            update_post_meta( $post_id, '_stock_status', 'instock');
            // update_post_meta( $post_id, 'total_sales', '0' );
            // update_post_meta( $post_id, '_downloadable', 'no' );
            // update_post_meta( $post_id, '_virtual', 'yes' );
            update_post_meta( $post_id, '_regular_price', 0);
            update_post_meta( $post_id, '_sale_price', 0);
            // update_post_meta( $post_id, '_purchase_note', '' );
            // update_post_meta( $post_id, '_featured', 'no' );
            // update_post_meta( $post_id, '_weight', '' );
            // update_post_meta( $post_id, '_length', '' );
            // update_post_meta( $post_id, '_width', '' );
            // update_post_meta( $post_id, '_height', '' );
            // update_post_meta( $post_id, '_sku', $items['lookup_code'] );
            // update_post_meta( $post_id, '_product_attributes', array() );
            // update_post_meta( $post_id, '_sale_price_dates_from', '' );
            // update_post_meta( $post_id, '_sale_price_dates_to', '' );
            update_post_meta( $post_id, '_price', $items['default_price']);
            // update_post_meta( $post_id, '_sold_individually', '' );
            // update_post_meta( $post_id, '_manage_stock', 'no' );
            // update_post_meta( $post_id, '_backorders', 'no' );
            // update_post_meta( $post_id, '_stock', '' );
            // update_post_meta( $post_id, '_', '' );


            // ===================== Create product image =================
            $image = $items['cover_img_url'];

            // magic sideload image returns an HTML image, not an ID
            $media = media_sideload_image($image, $post_id);

            // therefore we must find it so we can set it as featured ID
            if(!empty($media) && !is_wp_error($media)){
                $args = array(
                    'post_type' => 'attachment',
                    'posts_per_page' => -1,
                    'post_status' => 'any',
                    'post_parent' => $post_id
                );

                // reference new image to set as featured
                $attachments = get_posts($args);

                if(isset($attachments) && is_array($attachments)){
                    foreach($attachments as $attachment){
                        // grab source of full size images (so no 300x150 nonsense in path)
                        $image = wp_get_attachment_image_src($attachment->ID, 'full');
                        // determine if in the $media image we created, the string of the URL exists
                        if(strpos($media, $image[0]) !== false){
                            // if so, we found our image. set it as thumbnail
                            set_post_thumbnail($post_id, $attachment->ID);
                            // only want one image
                            break;
                        }
                    }
                }
            }


            // ================= Create Product Gallery
            $images = $items['images'];
            if(isset($images) && is_array($images)){

                $list_id = "";

                // magic sideload image returns an HTML image, not an ID
                $media = media_sideload_image($images[1]['full_url'], $post_id);

                // therefore we must find it so we can set it as featured ID
                if(!empty($media) && !is_wp_error($media)){
                    $args = array(
                        'post_type' => 'attachment',
                        'posts_per_page' => -1,
                        'post_status' => 'any',
                        'post_parent' => $post_id
                    );

                    // reference new image to set as featured
                    $attachments = get_posts($args);

                    if(isset($attachments) && is_array($attachments)){
                        foreach($attachments as $attachment){
                            // grab source of full size images (so no 300x150 nonsense in path)
                            $image = wp_get_attachment_image_src($attachment->ID, 'full');
                            // determine if in the $media image we created, the string of the URL exists
                            if(strpos($media, $image[0]) !== false){
                                // if so, we found our image. set it as thumbnail
                                // set_post_thumbnail($post_id, $attachment->ID);
                                $list_id .= $attachment->ID . ",";
                                // only want one image
                                break;
                            }
                        }
                    }
                }

                update_post_meta($post_id,'_product_image_gallery',$list_id);

Archive-Product.php code

<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce/document/template-structure/
 * @package WooCommerce/Templates
 * @version 3.4.0
 */

defined( 'ABSPATH' ) || exit;

get_header( 'shop' );


/**
 * Hook: woocommerce_before_main_content.
 *
 * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
 * @hooked woocommerce_breadcrumb - 20
 * @hooked WC_Structured_Data::generate_website_data() - 30
 */
do_action( 'woocommerce_before_main_content' );

?>
<div class="site-main-product">
<header class="woocommerce-products-header">
    <?php /*if ( apply_filters( 'woocommerce_show_page_title', true ) ) :*/ ?>
        <!-- <h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1> -->
    <?php /*endif;*/ ?>

    <?php
    /**
     * Hook: woocommerce_archive_description.
     *
     * @hooked woocommerce_taxonomy_archive_description - 10
     * @hooked woocommerce_product_archive_description - 10
     */
    /*do_action( 'woocommerce_archive_description' );*/
    ?>

</header>
<?php
if ( woocommerce_product_loop() ) {

    /**
     * Hook: woocommerce_before_shop_loop.
     *
     * @hooked wc_print_notices - 10
     * @hooked woocommerce_result_count - 20
     * @hooked woocommerce_catalog_ordering - 30
     */
    /*do_action( 'woocommerce_before_shop_loop' );*/
    ?>

    <div class="shop-page-notices">
        <div class="woocommerce-notices-shortcode woocommerce">
            <?php wc_print_notices(); ?>
        </div>
    </div>

    <?php if(is_shop()) : ?>
        <div class="shop-title">
            Best Rental Item
        </div>

    <?php else : ?>
        <div class="shop-title">
            <?php echo apply_filters( 'woocommerce_categories_header_title', wp_title( '»', false ) ); ?>
        </div>
    <?php endif; ?> 

    <?php

    if ( wc_get_loop_prop( 'total' ) ) {

        $taxonomy = 'product_cat';
        $taxonomy_terms = get_terms( $taxonomy );

        foreach ($taxonomy_terms as $key => $value) {
            $is_displayed = false;
            while ( have_posts() ) {
                the_post();

                $brands = get_the_terms(get_the_ID(), $taxonomy);

                if(!empty($brands)){
                    foreach ($brands as $key => $brand) {
                        if($brand->term_taxonomy_id==$value->term_id){
                            $is_displayed = true;
                            break;
                        }
                    }

                }
            }
            if($is_displayed==true){
                $is_displayed = false;
            ?>  

            <div class="shop-archive-brand-title">
                <img src="<?php echo $imageURL = wp_get_attachment_image_url(get_term_meta($value->term_id, 'thumbnail_id', true), 'medium'); ?>" style="height: 60px;"/>
                <div class="shop-archive-brand-title-middle-line">
                    <hr>
                </div>
            </div>

            <?php

                woocommerce_product_loop_start();
                while ( have_posts() ) {
                    the_post();

                    $brands = get_the_terms(get_the_ID(), $taxonomy);

                    if(!empty($brands)){
                        foreach ($brands as $key => $brand) {
                                        //var_dump($value);
                            if($brand->term_taxonomy_id==$value->term_id){
                                do_action( 'woocommerce_shop_loop' );
                                wc_get_template_part( 'content', 'product' );
                            }
                        }
                    }
                }
            }
            woocommerce_product_loop_end();
        }
    }



    /**
     * Hook: woocommerce_after_shop_loop.
     *
     * @hooked woocommerce_pagination - 10
     */
    do_action( 'woocommerce_after_shop_loop' );
} else {
    /**
     * Hook: woocommerce_no_products_found.
     *
     * @hooked wc_no_products_found - 10
     */
    do_action( 'woocommerce_no_products_found' );
}

/**
 * Hook: woocommerce_after_main_content.
 *
 * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
 */
do_action( 'woocommerce_after_main_content' );

/**
 * Hook: woocommerce_sidebar.
 *
 * @hooked woocommerce_get_sidebar - 10
 */
?>
</div>

<?php
do_action( 'woocommerce_sidebar' );

get_footer( 'shop' );
Share Improve this question edited Jan 29, 2019 at 7:44 Gufran Hasan 6918 silver badges20 bronze badges asked Jan 29, 2019 at 6:43 Yves GonzagaYves Gonzaga 53 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

Try like

$post_id = wp_insert_post( array(
    'post_title' => $items['description'],
    'post_content' => $items['detail'],
    'post_excerpt' => $items['description_2'],
    'post_status' => 'publish',
    'post_type' => "product",
) );

// Set Product type
// wp_set_object_terms( $post_id, 'simple', 'product_type');                            
wp_set_post_terms( $post_id, 'simple', 'product_type');
// Get parent category returns mixed or objects
$category_parent = get_term_by('name', 'Lense', 'product_cat');
// Get child category returns array
$category_child = term_exists( 'Canon', 'product_cat', $category_parent->term_id);

// Set product brand
// wp_set_object_terms( $post_id, 'Canon', 'product_brand');
// wp_set_post_terms( $post_id, 'Canon', 'product_brand');

// Set category for this product post
// wp_set_object_terms( $post_id, $category_parent->term_id, 'product_cat', false); 
// wp_set_object_terms( $post_id, (int)$category_child['term_id'], 'product_cat', true);

wp_set_post_terms($post_id, array($category_parent->term_id,(int)$category_child['term_id']), 'product_cat');


// add_post_meta( $post_id, '_wc_booking_availability', $availability );

update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0');
update_post_meta( $post_id, '_downloadable', 'yes');
update_post_meta( $post_id, '_virtual', 'yes');
update_post_meta( $post_id, '_regular_price', "1" );
update_post_meta( $post_id, '_sale_price', "1" );
update_post_meta( $post_id, '_purchase_note', "" );
update_post_meta( $post_id, '_featured', "no" );
update_post_meta( $post_id, '_weight', "" );
update_post_meta( $post_id, '_length', "" );
update_post_meta( $post_id, '_width', "" );
update_post_meta( $post_id, '_height', "" );
update_post_meta($post_id, '_sku', "");
update_post_meta( $post_id, '_product_attributes', array());
update_post_meta( $post_id, '_sale_price_dates_from', "" );
update_post_meta( $post_id, '_sale_price_dates_to', "" );
update_post_meta( $post_id, '_price', "1" );
update_post_meta( $post_id, '_sold_individually', "" );
update_post_meta( $post_id, '_manage_stock', "no" );
update_post_meta( $post_id, '_backorders', "no" );
update_post_meta( $post_id, '_stock', "" );




// ===================== Create product image =================
$image = $items['cover_img_url'];

// magic sideload image returns an HTML image, not an ID
$media = media_sideload_image($image, $post_id);

// therefore we must find it so we can set it as featured ID
if(!empty($media) && !is_wp_error($media)){
    $args = array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'post_status' => 'any',
        'post_parent' => $post_id
    );

    // reference new image to set as featured
    $attachments = get_posts($args);

    if(isset($attachments) && is_array($attachments)){
        foreach($attachments as $attachment){
            // grab source of full size images (so no 300x150 nonsense in path)
            $image = wp_get_attachment_image_src($attachment->ID, 'full');
            // determine if in the $media image we created, the string of the URL exists
            if(strpos($media, $image[0]) !== false){
                // if so, we found our image. set it as thumbnail
                set_post_thumbnail($post_id, $attachment->ID);
                // only want one image
                break;
            }
        }
    }
}


// ================= Create Product Gallery
$images = $items['images'];
if(isset($images) && is_array($images)){

    $list_id = "";

    // magic sideload image returns an HTML image, not an ID
    $media = media_sideload_image($images[1]['full_url'], $post_id);

    // therefore we must find it so we can set it as featured ID
    if(!empty($media) && !is_wp_error($media)){
        $args = array(
            'post_type' => 'attachment',
            'posts_per_page' => -1,
            'post_status' => 'any',
            'post_parent' => $post_id
        );

        // reference new image to set as featured
        $attachments = get_posts($args);

        if(isset($attachments) && is_array($attachments)){
            foreach($attachments as $attachment){
                // grab source of full size images (so no 300x150 nonsense in path)
                $image = wp_get_attachment_image_src($attachment->ID, 'full');
                // determine if in the $media image we created, the string of the URL exists
                if(strpos($media, $image[0]) !== false){
                    // if so, we found our image. set it as thumbnail
                    // set_post_thumbnail($post_id, $attachment->ID);
                    $list_id .= $attachment->ID . ",";
                    // only want one image
                    break;
                }
            }
        }
    }

    update_post_meta($post_id,'_product_image_gallery',$list_id);

Hope it will help !

Post a comment

comment list (0)

  1. No comments so far