WordPress.org

Make WordPress Core

Opened 5 months ago

Last modified 7 days ago

#40807 reviewing enhancement

Starter content: Allow for setting a "parent" for pages (and other hierarchical CPTs)

Reported by: webmandesign Owned by: westonruter
Milestone: 4.9.1 Priority: normal
Severity: normal Version: 4.7
Component: Customize Keywords: has-patch has-unit-tests
Focuses: Cc:

Description (last modified by westonruter)

From what I can see in get_theme_starter_content() function, it only allows for specific fields to be set for posts: post_type, post_title, post_excerpt, post_name, post_content, menu_order, comment_status, thumbnail and template.

I would like to propose adding a parent field in there too for hierarchical post types, such as Pages.

That way we can also create child pages (or sub-pages) in starter content, such as:

<?php
$starter_content = array(

  'posts' => array(

    'contact' => array(
      'template' => 'page-templates/list-of-child-pages.php',
    ),

    'contact-dublin' => array(
      'post_type'    => 'page',
      'post_title'   => 'Dublin Office Contact',
      'post_content' => 'Page content here...',
      'parent'       => '{{contact}}', // <-- Here's the magic:
                                       //     Making this a child page of our "Contact" page.
    ),

  ),

);

Thanks for consideration!

Attachments (4)

40807.patch (3.9 KB) - added by birgire 2 weeks ago.
40807.2.patch (4.5 KB) - added by birgire 2 weeks ago.
40807.3.diff (4.4 KB) - added by westonruter 12 days ago.
Δ https://github.com/xwp/wordpress-develop/pull/276/commits/0bfc4dc
40807.4.diff (4.5 KB) - added by birgire 12 days ago.

Download all attachments as: .zip

Change History (13)

#1 @westonruter
5 months ago

  • Description modified (diff)
  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release
  • Version changed from trunk to 4.7

Good call. Yes, this makes great sense.

@birgire
2 weeks ago

#2 @birgire
2 weeks ago

  • Keywords has-patch has-unit-tests added; needs-patch removed

Sounds like a good idea.

Here's a first attempt to support setting a parent post symbol.

In 40807.patch

  • Translate the parent post symbol to an ID and assign it to the post_parent field with wp_update_post()
  • Uses the parent key for symbols, to not confuse it with post_parent that only supports an integer value.
  • Includes a test, based on the existing one for starter-content

@birgire
2 weeks ago

#3 @birgire
2 weeks ago

In 40807.2.patch

  • Added a deeper test config tree.
  • Added a failing (non-existing) parent post symbol, in the test config.
  • Use get_post_field() to get the post_parent field.

To consider:

  • Add a hierarchical post type restriction on posts using the parent
  • Add a same parent/child post type restriction on posts using the parent


Last edited 2 weeks ago by birgire (previous) (diff)

#4 @westonruter
12 days ago

  • Milestone changed from Future Release to 4.9
  • Owner set to westonruter
  • Status changed from new to reviewing

#5 @westonruter
12 days ago

@birgire patch is looking good! I don't think we need to worry about restricting which post types can have children. I think this hierarchical property is really more for the sake of the UI than a restriction on the data model. After all, an attachment post can have parent even though it isn't hierarchical.

See my changes for what needed to be done to apply WordPress-Coding-Standards. It was mostly whitespace changes; see diff without.

A build is running now: https://travis-ci.org/xwp/wordpress-develop/builds/284338738

While this was marked as an enhancement, I'm inclined to include it in 4.9 as it is closely related to #39254.

Last edited 12 days ago by westonruter (previous) (diff)

#6 @birgire
12 days ago

Thank you @westonruter for the whitespace cleanup and the other info.

I will consult the phpcs --standard=WordPress ... when writing patches from now on ;-)

I agree with your reasoning regarding the consideration of the two restrictions.

@birgire
12 days ago

#7 @birgire
12 days ago

It looks like I forgot the {{...}} syntax, in the previous patch.

In 40807.4.diff we assume the parent reference to be like:

'animal' => array(
    'post_type'  => 'page',
    'post_title' => 'Animal',
),
'fish'   => array(
    'post_type'  => 'page',
    'post_title' => 'Fish',
    'parent'     => '{{animal}}',
),

This ticket was mentioned in Slack in #core by melchoyce. View the logs.


10 days ago

#9 @westonruter
7 days ago

  • Milestone changed from 4.9 to 4.9.1

Punting this to the next minor release along with #39254, due to the number of outstanding tickets and lack of availability to focus on this.

Note: See TracTickets for help on using tickets.