开发者

get multiple categories from xml-data and use it in wp_insert_post post_category

开发者 https://www.devze.com 2023-02-21 13:03 出处:网络
I am using the following while loop inside an foreach-loop to get categories from an xml file: foreach($mm_rss_xml->channel->item as $mm_item)开发者_如何学运维 {

I am using the following while loop inside an foreach-loop to get categories from an xml file:

foreach($mm_rss_xml->channel->item as $mm_item)开发者_如何学运维 {

$i = 0;
while ($xml_getinfo_result->movies->movie->categories->category[$i]) {
$tmdb_cats = $xml_getinfo_result->movies->movie->categories->category[$i]->attributes()->name;  // TMDb Categories
echo "<li>".tmdb_cats."</li>";
$i++;
}

gives me:

  • action
  • drama
  • thriller

Now, how can I add these categories into wordpress categories? (they are already added in wordpress, so they don't need to be added anymore)

I use the following for adding the post into WP:

$my_post = array();
    $my_post['post_title'] = $tmdb_moviename;
    $my_post['post_content'] = $mm_overview;
    $my_post['post_category'] = // I am stuck here...
    $my_post['post_status'] = 'publish';
    $my_post['post_author'] = 1;
    $my_post['tags_input'] = $tmdb_actors2;
    $my_post['filter'] = true;

$posted_id = wp_insert_post($my_post);

add_post_meta($posted_id, 'test', $average_rating);

} // end foreach loop

Thanks in advance for any answers :)


I figured it out myself:

$cats = array();
while ($xml_getinfo_result->movies->movie->categories->category[$i]) {
    $tmdb_cats = $xml_getinfo_result->movies->movie->categories->category[$i]->attributes()->name; // TMDb Categories
    array_push($cats, get_cat_ID($tmdb_cats));
echo "<li>".tmdb_cats."</li>";
    $i++;   
}

Then put the array here:

$my_post = array(); // build the needed array for the post itself
    $my_post['post_title'] = $tmdb_moviename;
    $my_post['post_content'] = $mm_overview;
    $my_post['post_category'] = $cats;
    $my_post['post_status'] = 'publish';
    $my_post['post_author'] = 1;
    $my_post['tags_input'] = $tmdb_actors2;
    $my_post['filter'] = true;
0

精彩评论

暂无评论...
验证码 换一张
取 消