importing parent/child hierarchies for pages

Closed

Import WP Updated 1 year ago 3 Replies

Howie asked 1 year ago on January 11, 2023 at 8:57 pm

Hi,

As per https://www.importwp.com/docs/importer/how-to-guides/how-to-import-wordpress-pages-with-hierarchy-from-csv-file/ how do I create the parent/child association for pages please?

Your sample csv page throws a 404: https://www.importwp.com/wp-content/themes/importwp/assets/files/404.html

I have a column like:

/url1/url2/url3

/page1/page2/page3

 

———————————————–

 

and would like to create my page hierarchies like that:

page1

>page2

>>page3

 

How do I format the column to work with this plugin?

Thanks

 

 

J

James replied Support Agent

1 year ago on January 11, 2023 at 11:04 pm

Hi Howie,

I have attached the csv file for that example.

The "parent field" allows you to set a parent page for the currently imported page by either referencing the Parents ID, Title/name, slug, or a specific column value in your csv file.

Based on your example, ideally a stripped back version of your csv would be like the following:

Name, Slug, Parent Slug
Page 1, page-1, 
Page 2, page-2, page-1
Page 3, page-3, page-2

In this example you would use the following parent settings:

- Parent: {3}
- Parent Field Type: slug

Please make sure that you have enabled the "status" field and set it to "publish", as parents can only reference published pages.

If you are set on using a column with the values: "/page1/page2/page3", would your example csv be like the following:

Name, Slug, Content, Parent
Page 1, page1, Example page 1 content, /page1
Page 2, page2, Example page 2 content, /page1/page2
Page 3, page3, Example page 3 content, /page1/page2/page3 

This could be accomplish by using a custom method to parse the "Parent" column "/page1/page2/page3" into a usable value e.g. "page2" which would then reference the parent slug.

HC

Howie Cann replied

1 year ago on January 12, 2023 at 9:06 am

Ok thanks for getting back. I'll look into how to apply a method further. I thought the plugin may natively have the functionality to derive the parent from a file path like "/page1/page2/page3" as the value in the parent column. Logic to assign the parent to the string between the last 2 slashes or something which would mean page2 is the parent of page3 and on. Trying to move 400 pages from a Drupal install to WP and create the Page parent/child relationships so looking for convenient ways to do it with a plugin.Anyway just a thought. Thanks 
J

James replied Support Agent

1 year ago on January 12, 2023 at 9:33 am

If you are set on the /page1/page2/page3 column, you could use the following custom method:

function iwp_get_parent_from_path($path = '')
{

    $parts = array_values(array_filter(array_map('trim', explode('/', $path))));
    if (!empty($parts) && count($parts) > 1) {
        return $parts[count($parts) - 2];
    }

    return '';
}

you can use this custom method by wrapping it round a csv column e.g. instead of {3} you would use:

[iwp_get_parent_from_path("{3}")]

Replacing {3} with the column you need to use.

Here is a example list of results passing some page paths into it, as you an see it returns the parent slug of the current path:

Input: / , Output: 
Input: /page1 , Output:
Input: /page1/page2 , Output: page1
Input: /page1/page2/page3 , Output: page2