Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i tried to generate a page via wp_insert_post. That works fine except that the post_name is always written with the same value as the post_title. Here comes the code snippet:

// Create homesite if not exists

$ins_home = array(
    'post_title' => 'Home',
    'post_name' => 'my-home-site',
    'post_status' => 'publish',
    'post_type' => 'page',
    'comment_status' => 'closed',
    'ping_status' => 'closed'
);

$result = $wpdb->query("SELECT wpost.post_name FROM $wpdb->posts wpost WHERE wpost.post_name = 'my-home-site'");

if($result < 1){// Insert the post into the database
    $page_id = wp_insert_post( $ins_home );
}

Any help is very appreciated.

Thanks a lot, Joe

share|improve this question
    
I just tried the original code and it works. I don't think it's the code that wrong. Something else may be messing with code. –  WP Themes Jan 18 '13 at 12:34

1 Answer 1

Gr8 job... only thing missing is the action and the timing for it to work...

try this:

$ins_home = array(
    'post_title'    =>  'Home',
    'post_name'     =>  'my-home-site',
    'post_status'   => array('publish'),
    'post_type'     => 'page',
    'comment_status' => 'closed',
    'ping_status' => 'closed'
);

$ins_home_id = wp_insert_post($ins_home, 10, 1);

$result = $wpdb->query("SELECT wpost.post_name FROM $wpdb->posts wpost WHERE wpost.post_name = 'my-home-site'");

if($result < 1){// Insert the post into the database
    do_action('wp_insert_post', 'wp_insert_post', 10, 1); 
}

.

EDIT According to the Source code of wp_insert_post

if ( empty($data['post_name']) && !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
    $data['post_name'] = sanitize_title($data['post_title'], $post_ID);
    $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
}

So... if post_status not in array... post_name = post_title (sanitized ofCourse)
So... altough i am very short on time to check it today you should try to envelope
the 'publish' post_status with an aray - see my revised code

.

i hope this solves it... please try it and share results

share|improve this answer
    
Sagive SEO, thanks a lot for your answer. I tried your code. But still the same result. The post_name has the same value as the post_title. (just in lowercases). –  JSS Aug 20 '12 at 1:42
    
i had a look in the source code of wp_insert_post and revised my answer - hope this works... got to run to my job - would check back later –  Sagive SEO Aug 20 '12 at 7:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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