0
votes

I'm importing some static pages into my WP website via code, and there's 2 issues I can't find any solution for.

Here's my insertion code:

$data = getPost(1);

// details for the post we're about to insert
$my_post = array(
    'post_title'  => $data['page_title'],
    'post_date'     => date('Y-m-d H:i:s',strtotime($data['date'])),
    'post_date_gmt' => date('Y-m-d H:i:s',strtotime($data['date'])),
    'post_content' => '<p>' . $data['intro'] . '</p>' . $data['body'],
    'post_status' => 'publish',
    //'post_category' => (!empty($cats[$data['category']]) ? $cats[$data['category']] :     1),
    'post_author' => (!empty($users[$data['author']]) ? $users[$data['author']] : 9),
    'post_name' => $data['slug'],
    'post_type' => 'post'
);

If you ever wonder what's inside of $data, here it is:

Array ( [post_title] => Gutscheine bei Bingo3X gewinnen [post_date] => 2013-12-08 00:00:00 [post_date_gmt] => 2013-12-08 00:00:00 [post_content] => Nicht nur Geld gewinnen macht Spaß, sondern Gutscheine können ebenfalls ihren Vorteil haben. So gibt es mit der Pouch-A-Vouch Aktion bei Bingo3X zurzeit Gutscheine im Wert von 30 £ gewinnen.

Nicht nur Geld gewinnen macht Spaß, sondern Gutscheine können ebenfalls ihren Vorteil haben. So gibt es mit der Pouch-A-Vouch Aktion bei Bingo3X zurzeit Gutscheine im Wert von 30 £ gewinnen.

[post_status] => publish [post_author] => 9 [post_name] => gutscheine-bei-bingo3x-gewinnen [post_type] => post )

  1. According to http://codex.wordpress.org/Function_Reference/wp_insert_post "post_name" will create the slug for the post (link to it), whenever I import this data, it adds the post correctly, but appends some weird number to the end of the slug, f.e it can be: mysite.com/2013/10/08/gutscheine-bei-bingo3x-gewinnen-5 <--- THAT -5 is coming from nowhere.. Any ideas? :)

  2. Bigger issue. How do I add featured_image via code? I have URL (or I can have the image located locally, I have sizes, but I don't find how to insert it with the post. Some kind of add_post_meta or wp_set_object_terms can help? But I'm not sure what to write there...

Thanks!!

1
Slugs must be unique, so if the post name is duplicate, slug must be altered in order for the post to be validnice ass
Have you tried this? codex.wordpress.org/Function_Reference/set_post_thumbnail Also, there is a simmilar question at wordpress.stackexchange.com/questions/40301/… try reading more about itAdam Baranyai
before wp_insert_data check if slug dosnt already existsPrince Singh

1 Answers

1
votes
    if( null == get_page_by_title( $title_connection ) ) { 
// $title_connection should be unique

            $post_id = wp_insert_post(

                array(

                    'comment_status'    =>  'closed',

                    'ping_status'       =>  'closed',

                    'post_author'       =>  $author_id,

                    'post_name'         =>  $slug_connection,

                    'post_title'        =>  $title_connection,

                    'post_status'       =>  'publish',

                    'post_type'         =>  'page',

                    'post_parent'       =>  $page_parent->ID

                )

            );



        }

To insert post featured image programmitically ...

 $wp_filetype = wp_check_filetype(basename($filename), null );

    $attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
    'post_content' => '',
    'post_status' => 'inherit'
    );

    $attach_id = wp_insert_attachment( $attachment, $filename, $parent_id );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );

add_post_meta($post_id, '_thumbnail_id', $attach_id, true);