D
D
Dmitry2014-11-28 22:07:22
PHP
Dmitry, 2014-11-28 22:07:22

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'] );

on the
$post_exists = post_exists( $post['post_name'], '', $post['post_date'] );

It didn't work.
Please tell me what needs to be changed in the plugin code so that it compares posts only by post_name.
Here is a snippet that I think needs to be edited:
pastie.org/9749126#3,5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Sydorenko, 2014-11-28
@1diem

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;
}

after which, in the plugin, on line 557 (as you wrote), the check will look like this
just note that you are also passing the date.. that is, the existing ones will only have those that have the same name, and !!exactly!! matching date. If the date for the sameness is not critical, then simply
He wrote quickly, perhaps he overlooked something, trying to convey the idea. I was hoping for your knowledge of php and Wordpress, once you got into the plugin code;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question