WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 4 months ago

#33045 reviewing enhancement

New conditional tags for child/parent pages

Reported by: ramiy Owned by: johnbillion
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch
Focuses: template Cc:
PR Number:

Description

New enhancements for theme developers with new conditional tags:

  • has_parent() - return boolean result using $post->post_parent.
  • parent_page() - return the parent page id using $post->post_parent.
  • is_child_of( $id ) - return boolean result using $post->post_parent.

We can change the names, but we need the functionality.

Attachments (6)

33045.patch (993 bytes) - added by ramiy 4 years ago.
33045.2.patch (967 bytes) - added by ramiy 4 years ago.
33045.3.patch (1.0 KB) - added by sebastian.pisula 4 years ago.
33045.4.patch (848 bytes) - added by ramiy 4 years ago.
33045.5.patch (848 bytes) - added by ramiy 5 months ago.
33045.6.patch (824 bytes) - added by ramiy 5 months ago.

Download all attachments as: .zip

Change History (33)

#1 @ramiy
4 years ago

See the patch.

Last edited 4 years ago by ramiy (previous) (diff)

@ramiy
4 years ago

#2 @ramiy
4 years ago

For consistency, the patch is based on the code of has_excerpt() and get_the_excerpt() functions.

@ramiy
4 years ago

#3 @ramiy
4 years ago

  • Keywords has-patch added

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


4 years ago

#5 follow-up: @johnbillion
4 years ago

  • Component changed from General to Posts, Post Types
  • Keywords reporter-feedback 2nd-opinion added

@ramiy What's the use case for filtering the return value of get_parent()?

Also these functions need post in their name to avoid ambiguity. eg. wp_get_post_parent().

#6 @sebastian.pisula
4 years ago

I added my version of patch

#7 in reply to: ↑ 5 @ramiy
4 years ago

  • Keywords reporter-feedback removed

Replying to johnbillion:

@ramiy What's the use case for filtering the return value of get_parent()?

Also these functions need post in their name to avoid ambiguity. eg. wp_get_post_parent().

You are right, there is no reason to filter the returned value. @sebastian.pisula revised my patch, he removed the filter and added the required post to the function name.

#8 @nir_r
4 years ago

+1 for this feature

#9 follow-up: @ramiy
4 years ago

Actually,

I don't agree with the new function name wp_get_post_parent(),
I prefer it would be get_post_parent(), without the wp_ prefix.

We should stick with the naming conventions in post-template.php, non of the get functions use the wp_ prefix:

  • get_the_ID()
  • get_the_title()
  • get_the_guid()
  • get_the_content()
  • get_the_excerpt()
  • get_post_class()
  • get_body_class()
Last edited 4 years ago by ramiy (previous) (diff)

@ramiy
4 years ago

#10 in reply to: ↑ 9 @maor
4 years ago

Love these functions! Great idea.

I actually just wrapped up a project where these functions could have been super helpful if existed.

Also, I agree with @ramiy on the naming convention. IMHO, I think there shouldn't be a wp_ prefix for the proposed conditional tags.

#11 @ramiy
4 years ago

Same logic can be applied for the boolean function, rename back from has_post_parent() to has_parent().

The other has_() functions don't use the post in their name:

  • has_excerpt()
  • has_tag()
  • has_term()
  • has_nav_menu()

Only one function use post in the name:

  • has_post_thumbnail()

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


4 years ago

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


2 years ago

#14 @SergeyBiryukov
2 years ago

  • Milestone changed from Awaiting Review to 4.9

#15 @ramiy
2 years ago

  • Keywords 2nd-opinion removed

@SergeyBiryukov Let's add this enhancement to 4.9. The ticket has a patch, it was tested and got a very good feedback from @johnbillion and @sebastian.pisula.

The only issue here was the function names. In the last patch I applied the naming conventions set in post-template.php to match similar template functions.

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


2 years ago

#17 @johnbillion
2 years ago

  • Owner set to johnbillion
  • Status changed from new to reviewing

#18 @johnbillion
2 years ago

I've decided these functions should be named get_parent_post() and has_parent_post(). The "parent post" is the object being gotten.

This also allows functions such as get_parent_term() to be introduced in the future in order to fetch a term's parent.

#19 @johnbillion
2 years ago

And just as I submitted that I questioned it. Maybe it needs to be get_parent_post_id(). Naming things is hard.

#20 @johnbillion
2 years ago

  • Milestone changed from 4.9 to Future Release

#21 @SergeyBiryukov
2 years ago

For reference, comment:28:ticket:24164 has a good overview of existing get_*() and the_*() template functions.

Until a new function is added, get_post_field( 'post_parent', $post ) could be used for the same purpose.

#22 @desrosj
5 months ago

  • Keywords needs-refresh added

@ramiy
5 months ago

#23 @ramiy
5 months ago

  • Keywords needs-refresh removed

@ramiy
5 months ago

#24 @ramiy
5 months ago

The last patch uses "short if" to match the code to get_the_ID().

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


5 months ago

#26 @birgire
5 months ago

In 33045.6.patch for the has_post_parent() docs, there's currently:

* @return int

but it should be:

 * @return bool

Regarding the naming, I would assume beforehand that get_post_parent() would return a WP_Post object, just as get_post().

But beside from that, we already have this function in core:

/**
 * Returns the ID of the post's parent.
 *
 * @since 3.1.0
 *
 * @param int|WP_Post $post Post ID or post object. Defaults to global $post.
 * @return int|false Post parent ID (which can be 0 if there is no parent), or false if the post does not exist.
 */
function wp_get_post_parent_id( $post ) {
        $post = get_post( $post );
        if ( ! $post || is_wp_error( $post ) ) {
                return false;
        }
        return (int) $post->post_parent;
}

See https://developer.wordpress.org/reference/functions/wp_get_post_parent_id/

Last edited 5 months ago by birgire (previous) (diff)

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


4 months ago

Note: See TracTickets for help on using tickets.