Answer the question
In order to leave comments, you need to log in
How to make WordPress compare posts by post_name when importing from xml?
Hello!
The situation is this: you need to import a lot of posts into a WordPress site through .xml files using the WordPress Importer plugin.
The problem is that this plugin compares their titles (post_title) and creation date (post_date) with the titles and date of existing posts on the site to avoid duplicate posts.
And I need such a check to be carried out by the name of the record (post_name). That is, if the name of any existing post matches the imported one, then the post will not be added.
I tried to make changes to the plugin file (its code: pastie.org/9749118#51), but it did not work.
I tried to replace in line 557
$post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
$post_exists = post_exists( $post['post_name'], '', $post['post_date'] );
Answer the question
In order to leave comments, you need to log in
Simply replacing post_title with post_name won't work. Here is the post_exist function code wpseek.com/function/post_exists You need to do exactly the same, only with a different name, and so that the check is not post_title in the body of this function, but post_name. Then the $post_exist variable will reflect the real existence of the post by name. Now you are passing in a name, but the function is comparing it to the title.
Approximately, add such a function (you can add it to the plugin file, since you have taken it up)
function post_exists_by_name($name, $content = '', $date = '') {
global $wpdb;
$post_name = wp_unslash( sanitize_post_field( 'post_name', $name, 0, 'db' ) );
$post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
$post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
$query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
$args = array();
if ( !empty ( $date ) ) {
$query .= ' AND post_date = %s';
$args[] = $post_date;
}
if ( !empty ( $name ) ) {
$query .= ' AND post_name = %s';
$args[] = $post_name;
}
if ( !empty ( $content ) ) {
$query .= 'AND post_content = %s';
$args[] = $post_content;
}
if ( !empty ( $args ) )
return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question