Preventing Duplicate Posts In FeedWordPress

Recently I had to setup an RSS aggregator for automatically importing posts from an RSS feed into a WordPress blog as a custom post type. The best free option for this seems to be FeedWordPress.

Unfortunately the plugin erroneously duplicated posts in my case for an unknown reason. Even though the same syndicated post with the same GUID / URL already existed in the database, it was re-imported on every feed update, thus leading to dozens of duplicate posts.

To fix the issue I made use of the “syndicated_item” filter hook that is provided by the plugin.

The filter function gets passed the RSS item and can return null in order to prevent the item from ever being imported. Therefore, the following code did the trick:

/**
 * Prevent the import of already existing posts via FeedWordPress
 */
function fwp_syndication_dedupe($item, $post)
{
    // URL of the original blog post
    $link = $item["guid"];

    // search for existing posts with the same URL
    $existing = new WP_Query(array(
        "post_status" => "publish",
        "meta_key" => "syndication_permalink",
        "meta_value" => $link
    ));

    // if a post already exists return null in order to prevent the import
    if ($existing->post_count) {
        return null;
    }

    // otherwise return the item unmodified and let FeedWordPress handle it as per usual
    return $item;
}

add_filter("syndicated_item", "fwp_syndication_dedupe");

One would expect that the plugin does this sort of check itself, but I didn’t have the time to check why it wasn’t. This was simply the quickest solution.