• Resolved timholz

    (@timholz)


    Hi
    I’am importing products and the corresponding images via json. I can set the category for each product with wp_set_object_terms($post_id, $cat_ids, 'product_cat');. Is it possible to assign an enhanced media library category for the image too?
    regards
    theo

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author webbistro

    (@webbistro)

    Hi Theo,

    Yes, media_category is an ordinary WordPress taxonomy. Use wp_set_object_terms( $post_id, $cat_ids, 'media_category' );

    Best,
    -Nadia

    Thread Starter timholz

    (@timholz)

    Hi Nadia
    Thanks a lot for the speedy response.
    Have a nice weekend.
    theo

    Thread Starter timholz

    (@timholz)

    Hi Nadia
    May i come back to my previously asked question?
    I tried wp_set_object_terms( $post_id, $cat_ids, 'media_category' ); and yes, it creates the desired category, but it does not select the checkbox. Is there any additional coding necessary, or is my function wrong?
    regards
    theo

    Plugin Author webbistro

    (@webbistro)

    Hi Theo,

    Sorry, the question is a bit unclear to me. Can you give me more information about the process? How do you need it to work step by step? Thanks!

    Best,
    -Nadia

    Thread Starter timholz

    (@timholz)

    Hi Nadia
    Thanks for your answer and sorry for the unclear question.
    I made a plugin that imports products automatically.
    Here is the essential code:

    register_activation_hook( __FILE__, 'do_products' );
    
    function insert_product ($product_data) {
    	$title = $product_data['title'];
    
    if (!get_page_by_title($title, 'OBJECT', 'product')){
        $post = array( // Set up the basic post data to insert for our product
    
            'post_author'  => 1,
    		'post_content' => $product_data['note'] . '<br>'. $product_data['note2'],
    		//'post_excerpt' => $product_data['note2'],
            'post_status'  => 'publish',
            'post_title'   => $product_data['title'],
            'post_type'    => 'product',
        );
    	
        $post_id = wp_insert_post($post); // Insert the post returning the new post id
    	
        if (!$post_id) // If there is no post id something has gone wrong so don't proceed
        {
            return false;
        }
    	update_post_meta($post_id, '_price', $product_data['price']); // Set its SKU
    	update_post_meta($post_id, '_regular_price', $product_data['price']);
    
    	
        update_post_meta( $post_id,'_visibility','visible'); // Set the product to visible, if not it won't show on the front end
    	$cat_ids = array( 40 );
        wp_set_object_terms($post_id, $cat_ids, 'product_cat'); // Set up its categories
        wp_set_object_terms($post_id, 'single', 'product_type'); // Set it to a single product type
    
    	    //add img section
                // only need these if performing outside of admin environment
                require_once(ABSPATH . 'wp-admin/includes/media.php');
                require_once(ABSPATH . 'wp-admin/includes/file.php');
                require_once(ABSPATH . 'wp-admin/includes/image.php');
    
                // example image
    	   $image = 'https://www.creatifpassion.ch/wp-content/import/testimg.gif';
    
                // 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);
                    //Here is the code that sets the category for the image
                    //but the checkbox isn't activated
                    wp_set_object_terms( $post_id, 'GA', 'media_category' );
    
                    if(isset($attachments) && is_array($attachments)){
                        foreach($attachments as $attachment){
                            
                            $image = wp_get_attachment_image_src($attachment->ID, 'full');
                            
                            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;
                            } //end if strpos
                        } //end foreach
                    } //end if isset
                }//end if empty
    	
    	}//end if
          
    }//end function
    
    function insert_products ($products)  {
        if (!empty($products)) // No point proceeding if there are no products
        {
            array_map('insert_product', $products); // Run 'insert_product' function from above for each product
        }
    }  
    function do_products(){
    $json_file = file_get_contents(plugin_dir_path( __FILE__ ) . 'json/tt_products3.json');
    strip_tags($json_file, '.');
    $products_data = json_decode($json_file, true); 
    
    insert_products($products_data);
    }
    add_action('init','do_products',100);
    ?>

    The upper part is just to show the context. In the ?add img section? i use:
    wp_set_object_terms( $post_id, 'GA', 'media_category' );
    It does insert a category for the image, but the checkbox isn’t activated.
    Is the code in wrong place?
    Thanks a lot for your interest.
    regards
    theo

    Plugin Author webbistro

    (@webbistro)

    Theo,

    From your code, it looks like you set media category to a product post, not to product attachments. Move it to the attachments loop and use wp_set_object_terms( $attachment->ID, 'GA', 'media_category' );. Also, better use media category ID instead of ‘GA’ like you do it for the product itself $cat_ids = array( 40 );

    Best,
    -Nadia

    Thread Starter timholz

    (@timholz)

    Hi Nadia
    Thanks a lot for your advice.
    I’ll try to change my code accordingly.
    regards
    theo

    Thread Starter timholz

    (@timholz)

    Hi Nadia
    I move the code to the attachement loop:

    $attachments = get_posts($args);
                    $imgcat_ids = array( 62 );//wp_set_object_terms( $post_id, 'GA', 'media_category' );
    
                    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');
                            wp_set_object_terms( $post_id, $imgcat_ids , 'media_category' );
                            // 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;
                            } //ende if strpos
                        } //ende foreach
                    } //ende if isset

    It only creats a category, but does not select the checkbox.
    Well, it’s allright.
    Thanks for your help.
    Regards
    theo

    Plugin Author webbistro

    (@webbistro)

    Theo,

    wp_set_object_terms( $attachment->ID, $imgcat_ids, 'media_category' );

    not

    wp_set_object_terms( $post_id, $imgcat_ids, 'media_category' );

    $post_id is not an attachment ID.

    Best,
    -Nadia

    Thread Starter timholz

    (@timholz)

    Hi Nadia
    Oh, thank you very much. This works well.
    Your plugin is great and so is your care.
    regards
    theo

    Plugin Author webbistro

    (@webbistro)

    Theo,

    You are welcome! Thank you for your kind words about the plugin.

    Best,
    -Nadia

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘wp_set_object_terms(‘);’ is closed to new replies.