1

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

  • What are you trying to do exactly? Create a bunch of posts, one YouTube video per post? – s_ha_dum Jun 1 '15 at 13:44
  • I've updated my goals: "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"" – mele Jun 1 '15 at 13:48
  • Could you not just store all these as a post of type youtube? wp_insert_post is your friend – Tom J Nowell Jun 1 '15 at 14:40
2

You already have your updateYoutube() function. Edit it to insert a new post. Very generally:

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 should, I believe, take care of the rest, though I have not tested it.

  • First of all, thank you. I will set the function in the functions.php page aight? Can I access this function only from pages in the WordPress itself? If I have a custom field like I mentioned above, should I just use this code: $my_post = array( 'post_title' => 'My post', 'video_url' => $videocode, 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array(8,39) – mele Jun 1 '15 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.