WP_INSERT_POST不会写入我的POST_NAME

时间:2012-08-19 作者:JSS

我试图通过wp\\u insert\\u post生成一个页面。这很好,只是post\\u名称的值始终与post\\u标题的值相同。下面是代码片段:

// 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 );
}
非常感谢您的帮助。

非常感谢Joe

1 个回复
SO网友:Sagive

Gr8作业。。。唯一缺少的是行动和它发挥作用的时机。。。

尝试以下操作:

$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根据wp\\U insert\\U 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 );
}
所以。。。如果post\\u状态不在数组中。。。post\\u name=post\\u标题(当然已清理)
所以。。。虽然我今天没有时间检查,但您应该尝试将“发布”post\\u状态封装在一个信封中,请参阅我的修订代码

我希望这能解决它。。。请尝试并共享结果

结束

相关推荐