1

I am importing products through a feed programmatically as follows:

$request = wp_remote_get('myapiurl');
$response = wp_remote_retrieve_body( $request );

// decode the json

$json = json_decode($response, true);

// loop through the results

if( ! empty($json[ 'results' ])) {
    foreach($json[ 'results' ] as $data) {

        // gather data you need, make sure to sanitize it

        $post_data = array(
              'post_title' => $data[ 'post_title' ],
                'post_name' => $data[ 'post_name' ],
                'post_content' => $data[ 'post_content' ],
                'post_category' => $data[ 'post_category' ],
                '_price' => $data[ 'price' ],
                'post_location' => $data[ 'location' ],
                'post_stock' => $data[ 'stock' ],
                'post_year' => $data[ 'year' ],
                'post_images' => $data[ 'images' ],
                'post_status' => 'publish',
                'post_type' => 'product',
        );

        // create product or post and return the new ID

        $post_id = wp_insert_post($post_data, $wp_error);
    }
}

The issue is that the image is not inserting as the featured image.

Kindly help me please

UPDATE :

Example: API Response (JSON)

{
    "status":"SUCCESS",
    "message":"Results have been extracted",
    "results":[{
                "post_title":"1984 AQUASPORT 222 FT W96 YAMAHA",
                "post_name":"1984-aquasport-22-2-ft-w96-ya",
                "post_content":"Nice boat 1984 aquasport 22.2 ft c.c 1996 yamaha 200 hp runs good.",
                "post_category":"By Owner",
                "price":"52000",
                "location":"Miami, FL",
                "stock":"952481",
                "year":"2007",
                "images":[
                          "http:\/\/boatexportusa.com\/wp-content\/plugins\/api\/get-image.php?src=fakesrc\/00o0o_7TGyKJXFMp9_600x450.jpg",
                          "http:\/\/boatexportusa.com\/wp-content\/plugins\/api\/get-image.php?src=fakesrc\/00o0o_7TGyKJXFMp9_600x450.jpg"
                         ],
                "cond":"new",
                "size":"27'",
                "make":"SeaRay",
                "model":"S300",
                "hours":"330",
                "propulsion":"Power",
                "addedtime":"2015-03-23"
                }]
}

UPDATE 1 :

[post_images] => Array
(
[0] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00k0k_l1jwqDulnwk_600x450.jpg
[1] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00k0k_l1jwqDulnwk_600x450.jpg
[2] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00303_cALZYkakJx2_600x450.jpg
[3] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/01515_5nEeIcoBGot_600x450.jpg
)

UPDATE 2:

Echo for : 'images' => $data[ 'images' ] :

[images] => Array
(
[0] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00d0d_3Qnz0HTaTVP_600x450.jpg
[1] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00d0d_3Qnz0HTaTVP_600x450.jpg
[2] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00202_apLgMytA6Fo_600x450.jpg
[3] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00J0J_2rxcE4rqK3h_600x450.jpg
[4] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00t0t_4PKqbSme4FE_600x450.jpg
[5] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00q0q_jbVdZ1mNi9R_600x450.jpg
[6] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00r0r_l2XOi3b8tML_600x450.jpg
[7] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00j0j_cHeK38ceFIG_600x450.jpg
[8] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00D0D_dqcnZzF1Msl_600x450.jpg
[9] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00606_aWYjRYKgX0g_600x450.jpg
[10] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00707_dfM9vlGIvcS_600x450.jpg
[11] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00U0U_jINezkwXHhY_600x450.jpg
)

Echo for $image_ids in your code :

Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
)
  • @jgraup updated in question – Raja Gopal Jan 5 '16 at 17:48
  • You mean the one which i updated now in the question as Update 1 : ? – Raja Gopal Jan 5 '16 at 18:01
0

You will need to get the images onto your server and into the library. From that point you will be able to attach them to the post. You can only have 1 featured image so this needs to be adjusted to set the featured once but still download all the images and attach them to the post.

I'm using media_sideload_image in this example but you could use wp_insert_attachment (there is another example on that page).

// the new post has been created
$post_id = wp_insert_post($post_data, $wp_error);

// now we'll deal with the images

$image_ids = array();

foreach($data[ 'images' ] as $inx => $url) {

    // add external image to our library
    $src = media_sideload_image(rawurldecode($url), $post_id, NULL, 'src');

    // convert to attachment src -> ID -> post
    $attach_id = attachment_url_to_postid($src);

    // grab the id so we can add to the post when we're done
    $image_ids[] = $attach_id;

    // access to attachment post
    // $attach_post = get_post($attach_id);

    // set the image's parent id to the post
    wp_update_post(array('ID' => $attach_id, 'post_parent' => $post_id));

    if($inx === 0) {
        // add the first image as the post's featured image
        add_post_meta($post_id, '_thumbnail_id', $attach_id, true);
    }
}

// add the ids onto the post's meta to use later -- store as simple string
add_post_meta($post_id, '_images', implode(',', $image_ids), true);

This code has not been tested


post_images is not a property of WP_Post so they need to be associated another way. Also many of those other properties should be added using add_post_meta instead of trying to add on the post itself - take a look at custom fields.

  • So post_images is correct for featured images wright? that need not need to be changed wright? – Raja Gopal Jan 5 '16 at 18:10
  • also , i need to add the code above // create product or post and return the new ID or below that – Raja Gopal Jan 5 '16 at 18:12
  • Thanks , So , whats the proper Member variable for featured images – Raja Gopal Jan 5 '16 at 18:17
  • what about $images => $data[ 'images' ], – Raja Gopal Jan 5 '16 at 18:22
  • Sorry for the late reply mate , i was out of work for few days . Just back . This code returns error so i changed as you said to wp_insert_attachment . then it results same as earlier without images . Also if the image gets locally downloaded then it should be on media gallery wright? but media gallery is empty – Raja Gopal Jan 14 '16 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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