forked from devgeniem/wp-geniem-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-post.php
80 lines (71 loc) · 2 KB
/
example-post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* An example of post importing.
*/
$data = new stdClass();
// Set the basic post data as an associative array and cast it to object.
$data->post = (object) [
'post_title' => 'The post title',
'post_content' => 'The post main content HTML.',
'post_excerpt' => 'The excerpt text of the post.',
];
// The array of attachments.
$data->attachments = [
[
'filename' => '123456.jpg',
'mime_type' => 'image/jpg',
'id' => '123456',
'alt' => 'Alt text is stored in postmeta.',
'caption' => 'This is the post excerpt.',
'description' => 'This is the post content.',
'src' => 'http://upload-from-here.com/123456.jpg',
],
];
// Postmeta data as key-value pairs.
$data->meta = [
'key1' => 'value1',
'_thumbnail_id' => 'gi_attachment_123456',
];
// Advanced custom fields data.
$data->acf = [
'name' => 'repeater_field_key',
'value' => [
[
'name' => 'repeater_value_1',
'value' => '...',
],
[
'name' => 'repeater_value_2',
'value' => '...',
],
],
[
'name' => 'single_field_key',
'value' => '...',
],
[
'name' => 'attachment_field_key',
'value' => 'gi_attachment_123456',
],
];
// Localization data.
$data->i18n = [
'locale' => 'en',
'master' => 'gi_id_56',
];
// Create a new instance by a unique id.
$api_id = 'my_custom_id_1234';
$post = new \Geniem\Importer\Post( $api_id );
// Set all the data for the post.
$post->set_data( $data );
// Try to save the post.
try {
// If the data was invalid or errors occur while saving the post into the dabase, an exception is thrown.
$post->save();
} catch ( \Geniem\Importer\Exception\PostException $e ) {
foreach ( $e->get_errors() as $scope => $errors ) {
foreach ( $errors as $key => $message ) {
error_log( "Importer error in $scope: " . $message );
}
}
}