我的目标是从WordPress安装中的一个文件夹导入所有XML文件(/data/*.xml)
为此,我在functions.php中添加了操作:
/**
* Show 'insert posts' button on backend
*/
add_action( "admin_notices", function() {
echo "";
echo "";
echo "To insert the posts into the database, click the button to the right.";
echo "Insert Posts";
echo "";
echo "";
});这是我的密码:
/**
* Create and insert posts from CSV files
*/
add_action( "admin_init", function() {
global $wpdb;
// I'd recommend replacing this with your own code to make sure
// the post creation _only_ happens when you want it to.
if ( ! isset( $_GET["insert_mti_posts"] ) ) {
return;
}
// Change these to whatever you set
$getposttype = array(
"custom-post-type" => "cikkek"
);
// Get the data from all those XMLs!
$posts = function() {
$xmlfiles = glob( __DIR__ . "/data/*.xml" );
$data = array();
$errors = array();
// Get array of XML files
foreach ( $xmlfiles as $key=>$xmlfile ) {
$xml = simplexml_load_file($xmlfile);
$xmldata = json_decode(json_encode($xml), true);
$posttitle = $xmldata['THIR']['CIM'];
$postlead = $xmldata['THIR']['LEAD'];
$postcontent = $xmldata['THIR']['HIRSZOVEG'];
$data = array(
$key => array(
"title" => $posttitle,
"description" => $postlead,
"content" => $postcontent
)
);
$data[] = $post;
};
if ( ! empty( $errors ) ) {
// ... do stuff with the errors
}
return $data;
};
// Simple check to see if the current post exists within the
// database. This isn't very efficient, but it works.
$post_exists = function( $title ) use ( $wpdb, $getposttype ) {
// Get an array of all posts within our custom post type
$posts = $wpdb->get_col( "SELECT post_title FROM {$wpdb->posts} WHERE post_type = '{$getposttype["custom-post-type"]}'" );
// Check if the passed title exists in array
return in_array( $title, $posts );
};
foreach ( $posts() as $post ) {
// If the post exists, skip this post and go to the next one
if ( $post_exists( $post["title"] ) ) {
continue;
}
// Insert the post into the database
$post["id"] = wp_insert_post( array(
"post_title" => $post["title"],
"post_content" => $post["content"],
"post_type" => $getposttype["custom-post-type"],
"post_status" => "draft"
));
}
});Issue 1:
代码类型可以工作,但only将第一个. .XML插入到WordPress数据库中。我不明白为什么,因为我循环遍历它们并发送一个数组。
Issue 2:代码检查给定->的标题,并将其与数据库匹配,如果它是相同的内容,->不应该添加它。不幸的是,确实如此。
Issue 3:我认为这是因为admin_init操作,但不幸的是,每次刷新admin_init时导入都会运行。我只想让它运行,如果我点击在管理中的插入职位按钮。还有更适合这种情况的钩子吗?
发布于 2021-02-17 12:58:36
您这样做是为了将帖子添加到$data数组中:
$data = ...这不是向数组中添加某些内容的方式。你想要追加,而不是分配。
想一想,就像写一张5件物品的清单,每一件物品你都把它扔进垃圾桶,拿一张新的纸,然后把它写在最上面。最后,您将得到一个只有最后一项的列表。
顺便提一下,您已经问了几个关于这段代码的问题,但是没有重构它,尽管它是以非常复杂的方式构造的。
相反,这样重写它会更容易、更快:
files = xml files in folder
foreach file
xmlfile = load the XML file
if the post does not exist
wp_insert_post( [ .... stuff from the XML file ... ] )而不是你现在拥有的:
data = empty array
get xml files function
files = xml files in folder
foreach file
xmlfile = load the XML file
add the details to an array
add that array to data
call the get xml files function and for each thing in the array it returns
if the post does not exist
call wp_insert_post with the details发布于 2021-02-17 16:39:07
谢谢大家的帮助。我就这样解决了:
在我添加的原始代码中,data[]变量没有被post数据填充。
这部分是因为语法错误(例如,循环返回错误数据时不需要$key)。
此外,XML返回额外的空格,因此需要添加一些regex,这是在json变量中处理的。
一旦XML正确地填充了data[]变量,导入就完美无缺了。
/**
* Show insert posts button on backend
*/
add_action( "admin_notices", function() {
echo "";
echo "";
echo "To insert the posts into the database, click the button to the right.";
echo "Insert Posts";
echo "";
echo "";
});
/**
* Create and insert posts from CSV files
*/
add_action( "admin_init", function() {
global $wpdb;
// I'd recommend replacing this with your own code to make sure
// the post creation _only_ happens when you want it to.
if ( ! isset( $_GET["insert_sitepoint_posts"] ) ) {
return;
}
// Change these to whatever you set
$sitepoint = array(
"custom-post-type" => "cikkek"
);
$posts = function() {
$xmlfiles = glob( __DIR__ . "/data/*.xml" );
$data = array();
$errors = array();
// Get array of XML files
foreach ( $xmlfiles as $key=>$xmlfile ) {
$xml = simplexml_load_file($xmlfile);
$json = preg_replace("/^\s+/", "", json_encode($xml));
$xmldata = json_decode($json, true);
$posttitle = $xmldata['THIR']['CIM'];
$postlead = $xmldata['THIR']['LEAD'];
$postcontent = $xmldata['THIR']['HIRSZOVEG'];
$post = array(
"title" => $posttitle,
"description" => $postlead,
"content" => $postcontent
);
array_push($data, $post);
};
if ( ! empty( $errors ) ) {
// ... do stuff with the errors
}
return $data;
};
// Simple check to see if the current post exists within the
// database. This isn't very efficient, but it works.
$post_exists = function( $title ) use ( $wpdb, $sitepoint ) {
// Get an array of all posts within our custom post type
$posts = $wpdb->get_col( "SELECT post_title FROM {$wpdb->posts} WHERE post_type = '{$sitepoint["custom-post-type"]}'" );
// Check if the passed title exists in array
return in_array( $title, $posts );
};
foreach ( $posts() as $post ) {
// If the post exists, skip this post and go to the next one
if ( $post_exists( $post["title"] ) ) {
continue;
}
// Insert the post into the database
$post["id"] = wp_insert_post( array(
"post_title" => $post["title"],
"post_content" => $post["content"],
"post_type" => $sitepoint["custom-post-type"],
"post_status" => "draft"
));
}
});https://wordpress.stackexchange.com/questions/383539
复制相似问题