admin管理员组文章数量:1303397
I'm trying to repurpose a code that worked for CSV files to do the same for XML-> That is import all files in the folder and create Wordpress custom posts(cikkek). So, I managed to parse the XML files in the given folder and get the right variables that I need:
$posttitle = $xml->THIR->CIM[0];
$postlead = $xml->THIR->LEAD[0];
$postcontent = $xml->THIR->HIRSZOVEG[0];
Now my question is, how can I assign these variables to the last part of the code (line 65 - wp_insert_post). I tried simply replacing '$post["title]",' with '$posttitle,' but the code does not seem to run.
Here's the whole code:
<?php
/**
* Create and insert posts from XML files
*/
add_action( "admin_init", function() {
global $wpdb;
if ( ! isset( $_GET["insert_mti_posts"] ) ) {
return;
}
// CPT SETTINGS
$mti = array(
"custom-post-type" => "cikkek"
);
// Get the data from all those XMLs!
$posts = function() {
$data = array();
$errors = array();
// Get array of XML files
$xmlfiles = glob( __DIR__ . "/data/*.xml" );
foreach ( $xmlfiles as $xmlfile ) {
$xml = simplexml_load_file($xmlfile);
$posttitle = $xml->THIR->CIM[0];
$postlead = $xml->THIR->LEAD[0];
$postcontent = $xml->THIR->HIRSZOVEG[0];
};
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, $mti ) {
// Get an array of all posts within our custom post type
$posts = $wpdb->get_col( "SELECT post_title FROM {$wpdb->posts} WHERE post_type = '{$mti["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" => $mti["custom-post-type"],
"post_status" => "draft"
));
}
});
*EDIT: I think the main issue is with $data. It should return an associative array like:
$data = array(
0 => array(
"title" => $posttitle[0],
"content" => $postcontent[0],
"description" => $postlead[0],
),
1 => array(
"title" => $posttitle[1],
"content" => $postcontent[1],
"description" => $postlead[1],
),
// ...
);
The issue is I don't know how to pass it this info.
Hope I explained it clearly. Any help is appreciated!
本文标签: XML FOLDERwpinsertposthow to assign XML variables to post
版权声明:本文标题:XML FOLDER - wp_insert_post - how to assign XML variables to post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741753900a2395997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论