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

3
  • What are you trying to do exactly? Create a bunch of posts, one YouTube video per post?
    – s_ha_dum
    Commented Jun 1, 2015 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
    Commented Jun 1, 2015 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
    Commented Jun 1, 2015 at 14:40

1 Answer 1

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.

1
  • 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
    Commented Jun 1, 2015 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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