我对Wordpress有点陌生,读了所有关于使用PHP创建批量帖子的文档都有点迷茫。我编写了一个小代码,使用Youtube API v3从Youtube频道获取所有视频,我想使用该循环和数据在wordpress中创建帖子。
如果有人需要我的代码进行任何形式的使用,我将在下面介绍我的代码。我将向您提供有关如何使用PHP创建群发帖子以及如何使用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();
?>
更新时间:
我正在尝试为每个新的$videocode(在循环中)创建帖子,并将$videocode插入到我的自定义帖子字段:“video\\u url”和一些静态/动态的“post\\u title”
提前感谢
最合适的回答,由SO网友:s_ha_dum 整理而成
你已经有了updateYoutube()
作用将其编辑为insert a new post. 一般而言:
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) {
// #################################
// NOTE: The following 99% cribbed from the wp_insert_post() entry in the Codex
// Create post object
$my_post = array(
\'post_title\' => \'My post\',
\'post_content\' => $videocode,
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_category\' => array(8,39) // Obviously illustrative only
);
// Insert the post into the database
wp_insert_post( $my_post );
// ##################################
return true;
} else {
return false;
}
}
}
The
auto-embedding system 我相信,应该照顾好其余的,尽管我还没有测试过。