Quantcast
Channel: Insert Featured image from Feed - WordPress Development Stack Exchange
Viewing all articles
Browse latest Browse all 2

Insert Featured image from Feed

$
0
0

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
)

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images