This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
Astra_Target_Rules_Fields::is_child_of_post( int $current_post_id, int $parent_post_id )
Check if a post/page is a child of another post/page (including descendants).
Description
This method checks both direct parent-child relationships and URL path patterns.
Parameters
- $current_post_id
-
(int) (Required) Current post/page ID.
- $parent_post_id
-
(int) (Required) Parent post/page ID to check against.
Return
(bool) True if current post is a child/descendant of parent post, false otherwise.
Source
File: classes/modules/target-rule/class-astra-target-rules-fields.php
private function is_child_of_post( $current_post_id, $parent_post_id ) {
if ( empty( $current_post_id ) || empty( $parent_post_id ) ) {
return false;
}
$current_post = get_post( $current_post_id );
if ( ! $current_post ) {
return false;
}
$post_parent = $current_post->post_parent;
while ( $post_parent ) {
if ( $post_parent == $parent_post_id ) {
return true;
}
$parent_post_obj = get_post( $post_parent );
$post_parent = $parent_post_obj ? $parent_post_obj->post_parent : 0;
}
$current_url = get_permalink( $current_post_id );
$parent_url = get_permalink( $parent_post_id );
if ( $current_url && $parent_url ) {
$current_path = rtrim( parse_url( $current_url, PHP_URL_PATH ), '/' );
$parent_path = rtrim( parse_url( $parent_url, PHP_URL_PATH ), '/' );
if ( 0 === strpos( $current_path, $parent_path . '/' ) ) {
return true;
}
}
return false;
}
Expand full source code Collapse full source code View on Trac
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |