I'm a bit new to Wordpress and I'm a bit lost reading all sort of docs about creating bulk posts using PHP. I've wrote a small code that using Youtube API v3 to get all the videos from a Youtube channel and I want to create post in wordpress using that loop and data.
My code is below if anyone needs it for any sort of usage and I will appriciate any help and explenation about how to create mass post using PHP and how to execute the code using wordpress.
<?php
$var = file_get_contents("test.txt"); // list of category IDs
$var = explode("\n", $var);
$servername = "localhost";
$username = "xxxxx";
$password = "xxxxx";
$dbname = "xxxxx";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function updateYoutube( $videocode ){
global $conn;
$run = $conn->query("SELECT `ID` FROM `youtubetable` WHERE `ID` = '$videocode'");
if ($run->num_rows > 0)
return false;
else
{
if ($conn->query("INSERT INTO youtubetable (`ID`) VALUES ('$videocode')") === TRUE)
return true;
else
return false;
}
}
foreach($var as $value)
{
$value = file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=$value&key={KEY}");
$value = json_decode($value);
$value = $value->items;
foreach( $value as $id) {
$playlist = $id->contentDetails->relatedPlaylists->uploads;
if(empty($playlist))
continue;
$playlist = file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=$playlist&key={KEY}&maxResults=50");
$playlist = json_decode($playlist);
$playlist = $playlist->items;
foreach( $playlist as $video) {
$videocode = $video->snippet->resourceId->videoId;
updateYoutube( $videocode );
}
}
}
$conn->close();
?>
Update:
I'm trying to create post for every new $videocode (in a loop) and insert the $videocode to a custom post field I have: "video_url" and a some static/dynamic "post_title"
Thanks in advance
youtube
?wp_insert_post
is your friend