@lancerok

Как программно добавить категории в wp?

Собственно сабж.

$id = '568303';


    $url = file_get_contents ('http://moonwalk.cc/api/videos.json?kinopoisk_id=' . $id. '&api_token=d10a8f4368bc3a6e26b11477ad939da1');
    $decode_url = json_decode($url, true);


    foreach($decode_url as $full_info){
        $title = $full_info['title_ru']; echo '<br>';
        $video = $full_info['iframe_url']; echo '<br>';		
        echo $full_info['year']; echo '<br>';	
        $description = $full_info['material_data']['description']; echo '<br>';		
	}

$poster =  'https://st.kp.yandex.net/images/film_big/' . $id. '.jpg';
//echo $poster ;


$cat = $full_info['material_data']['genres']; 
print_r ($cat);

foreach( $cat as $catid){
	echo $catid;
}

require_once( dirname(__FILE__) . '/wp-load.php' );
require_once( dirname(__FILE__) . '/wp-admin/includes/admin.php' );


$post_data = array(
    'post_title'    => $title,
    'post_content'  => $description,
    'post_status'   => 'publish',
    'post_author'   => 1,
);

// Create a post with required fields.
$post_id = wp_insert_post($post_data, true);
print_r($post_id);

// Категории
wp_set_object_terms( $post_id, array($catid), 'category' );


// acf video
$field_key = "video";
$value = $video;
update_field( $field_key, $value, $post_id );



// Set the value for the additional field:
// For example, take the numeric field "rating"
// Let's set the value to 80
update_post_meta($post_id , 'rating', 80);

// Add the "PostScript" string field
update_post_meta($post_id , 'post-script', 'Thank you for attention. Subscribe to my blog');


$url = $poster;  // Вставляем постер

// We will add it as the cover to the current post

$description = "Cover for my new post";
$file_array = array();
$tmp = download_url($url);

preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $url, $matches );
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;

$media_id = media_handle_sideload($file_array, $post_id, $description);

if (is_wp_error($media_id)) {
    @unlink($file_array['tmp_name']);
    echo $media_id->get_error_messages();
}

@unlink($file_array['tmp_name'] );

set_post_thumbnail($post_id, $media_id);


Кто подскажет почему присваивается только 1 последняя категория ?
чяднт?

Заранее спасибо.
  • Вопрос задан
  • 181 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы