在WordPress中,我正在尝试从CSV文件导入帖子。我想检查一下,有标题的帖子是否已经存在。我正在尝试使用数据库查询来实现这一点,但是我仍然能够从我的示例CSV文件中导入相同的三个帖子。
下面的PHP代码片段是我用来检查有标题的帖子是否已经存在的代码:
$check_post_exists = function( $title ) use ( $wpdb, $postTypeArray ) {
$posts = $wpdb->get_col( "SELECT post_title FROM {$wpdb->posts} WHERE post_type = '{$postTypeArray["custom-post-type"]}' AND post_status = 'publish'" );
return in_array( $title, $posts );
};
foreach ( $posts() as $post ) {
if ( $check_post_exists( $post["zoneid"] ) ) {
continue;
}
$post["id"] = wp_insert_post( array(
"post_title" => $post["zoneid"],
"post_content" => $post["bemaerkning"],
"post_type" => $postTypeArray["custom-post-type"],
"post_status" => "publish"
));
}我做错了什么,或者我遗漏了什么?
发布于 2019-01-18 17:41:05
使用此代码,并让我知道。
$my_post = array(
Array Data
);
$post_id = wp_insert_post($my_post);
add_post_meta($post_id, 'times', '1'); 我想这对你很管用
https://stackoverflow.com/questions/54251141
复制相似问题