我已经有了一个功能,因为我从csv文件获得帖子标题,但它以字符串形式出现,我该如何防止插入帖子重复
var $defaults = array(
'playername ' => null,
'country' => null,
'post_type' =>'player',
'year' => null,
'my_post_tags' => null,
'post_slug' => null,
'clubs' => null
);
function create_post($data, $options) {
$opt_draft = isset($options['opt_draft']) ? $options['opt_draft'] : null;
$opt_cat = isset($options['opt_cat']) ? $options['opt_cat'] : null;
$data = array_merge($this->defaults, $data);
$new_post = array(
'post_title' => convert_chars($data['playername']),
'post_status' => $opt_draft,
'post_type' => 'player',
'post_name' => $data['post_slug'],
);
$title = $data['playername'] ;
$id = wp_insert_post($new_post);
update_post_meta($id, 'club', convert_chars($data['clubs']));
update_post_meta($id, 'year', convert_chars($data['year']));
update_post_meta($id, 'year', convert_chars($data['country']));
return $id;
}
答案 0 :(得分:1)
if( null == get_page_by_title( convert_chars($data['clubs']), '','player' ) ) {
$new_post = array(
'post_title' => convert_chars($data['playername']),
'post_status' => $opt_draft,
'post_type' => 'player',
'post_name' => $data['post_slug'],
);
$title = $data['playername'] ;
$id = wp_insert_post($new_post);
update_post_meta($id, 'club', convert_chars($data['clubs']));
update_post_meta($id, 'year', convert_chars($data['year']));
update_post_meta($id, 'year', convert_chars($data['country']));
return $id;
}