From 389a0fbcbb05c5005a48dea42e23cb67d6899380 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Mon, 22 Jan 2024 15:25:30 +0900 Subject: [PATCH] importer: use page if a post and page clash with slugs This is due to an upstream issue with wordpress where if you set permalink to /%postname%/ it allows a page and post to have the same slug. But when viewing via the slug wordpress always picks the page. https://core.trac.wordpress.org/ticket/13459 --- .../importers/wordpress.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/wagtail_wordpress_import/importers/wordpress.py b/wagtail_wordpress_import/importers/wordpress.py index 8c056580..fd83b633 100644 --- a/wagtail_wordpress_import/importers/wordpress.py +++ b/wagtail_wordpress_import/importers/wordpress.py @@ -125,6 +125,27 @@ def run(self, *args, **kwargs): wp_post_id=wordpress_item.cleaned_data.get("wp_post_id") ) except self.page_model_class.DoesNotExist: + try: + # We are creating a new page as the wp_post_id doesn't exist + # however we may have a collision of slugs between page and post types + # https://core.trac.wordpress.org/ticket/13459 + slug_post = self.page_model_class.objects.get( + slug=wordpress_item.cleaned_data.get("slug") + ) + + # If the existing type is a post and the new type is a page + # then override by deleting the existing post + # https://core.trac.wordpress.org/ticket/13459 + if slug_post.wp_post_type == "post" and wordpress_item.cleaned_data.get("wp_post_type") == "page": + slug_post.delete() + # If the existing type is a page and the new type is a post + # then ignore as page is stronger than post + elif slug_post.wp_post_type == "page" and wordpress_item.cleaned_data.get("wp_post_type") == "post": + self.logger.skipped += 1 + continue + except self.page_model_class.DoesNotExist: + pass + page = self.page_model_class() # add categories for this page if categories plugin is enabled