Importing custom post types using WordPress Importer, how to check for meta field values

I have a custom post type that takes only the title field. Associated with it I have a custom meta field that takes a URL.

I am using the WordPress Importer plugin (http://wordpress.org/plugins/wordpress-importer/) to import posts from another site into this one, and by default WP checks whether there is the same post type and whether there are any same titles, in which case it skips over that record.

Read More

Now I not only want to check for title but also for that URL meta field, because in this case it is more of a unique identifier, how can I do that?

Related posts

1 comment

  1. I guess you want to modify these lines in the WordPress-Importer plugin:

    $post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
    if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
    

    to add some post meta checks.

    I don’t see any hooks inside the function post_exists() that we could use.

    Remark:

    I don’t recommend editing plugin files, but if this is a one time import and you can’t find any other solution, then you might give it a try.

    Idea:

    So you could try to replace the above lines with the following snippet:

    $post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
    
    //
    // if the post exists, let's check if it has the right post meta stuff
    //
    $my_meta_key = 'url'; // Edit this to your needs
    $meta_exists = FALSE;
    
    if ( $post_exists > 0 && isset( $post['postmeta'] ) ) 
    {
        foreach( $post['postmeta'] as $meta ) 
        {
            if ( $my_meta_key === $meta['key'] ) 
            {
                if( $meta['value'] === get_post_meta( $post_exists, $my_meta_key, TRUE ) )
                    $meta_exists = TRUE;
    
                break;
            }
        }
    }
    
    if ( $post_exists && $meta_exists && get_post_type( $post_exists ) == $post['post_type'] ) 
    {
       ...
    

    where you must modify the $my_meta_key to your needs.

    This idea is untested so remember to take a backup before testing.

Comments are closed.