Skip to content

Instantly share code, notes, and snippets.

@tekapo
Forked from wokamoto/example.php
Created November 16, 2012 05:37
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tekapo/4084471 to your computer and use it in GitHub Desktop.
Save tekapo/4084471 to your computer and use it in GitHub Desktop.
how to use class-wp_post_helper
<?php
require_once('/path/to/wordpress/wp-load.php');
require_once('class-wp_post_helper.php');
// initialize
$post = new wp_post_helper(array(
'post_name' => 'slug' , // slug
'post_author' => 1 , // author's ID
'post_date' => '2012/11/15 20:00:00' , // post date and time
'post_type' => 'posts' , // post type (you can use custom post type)
'post_status' => 'publish' , // post status, publish, draft and so on
'post_title' => 'title' , // post title
'post_content' => 'content' , // post content
'post_category'=> array(1, 2) , // category IDs in an array
'post_tags' => array('tag1', 'tag2') , // post tags in an array
));
// adding media
$post->add_media(
'/path/to/wordpress/wp-content/uploads/hoge.jpg' , // absolute path to the madia file
'tittle', // titile of the media
'description', // description of the media
'caption', // caption of the media
true // true (set it as a featured image) or false (don't set is as a featured image)
);
// Download and add a media from URL.
// remote_get_file() is in class-wp_post_helper.php.
// When you pass a URL to the argument, it will download the file to wp_upload_dir and return the absolute path.
// When it fails, return false.
if ( $media = remote_get_file('http://example.com/fuga.jpg') ) {
$post->add_media($media, 'title', 'description', 'caption', false);
}
// adding a value to a custom field
$post->add_meta(
'meta_key', // meta key
'meta_val', // meta value
true // add it as unique (true) or not (false)
);
// adding a value as a format of Advanced Custom Field
$post->add_field(
'field_xxxxxxxxxxxxx', // key
'field_val' // value
);
// post
$post->insert();
// post-processing
unset($post);
@dsmatilla
Copy link

Line 10 should be

'post_type' => 'post' ,

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment