Skip to content

Commit

Permalink
importer: use page if a post and page clash with slugs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ahayzen-kdab committed Jan 22, 2024
1 parent ec44ab5 commit feef216
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions wagtail_wordpress_import/importers/wordpress.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ 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":
continue
except self.page_model_class.DoesNotExist:
pass

page = self.page_model_class()

# add categories for this page if categories plugin is enabled
Expand Down

0 comments on commit feef216

Please sign in to comment.