在WordPress中,我试图从CSV文件导入posts。我想检查一下,是否已经存在带有标题的帖子。我试图使用数据库查询来实现这一点,但是我仍然能够从我的示例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 06:43:49
你打错了。您正在代码post_status = 'publish"的末尾使用双引号而不是单引号。
请使用post_status = 'publish'代替post_status = 'publish",然后在查询结束时使用双引号。
并通过在执行前打印查询,确保在查询中正确地输入了动态值。
https://wordpress.stackexchange.com/questions/325925
复制相似问题