L
L
lancerok2018-05-24 11:32:17
JSON
lancerok, 2018-05-24 11:32:17

How to programmatically add categories to wp?

Actually a subject.

$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);

Who will tell you why only 1 last category is assigned?
chyadnt?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question