I think what you need is:
<?php
$post_id = $post->ID; //or somehow get the current post ID.
$ancestors = get_post_ancestors($post_id)
//$ancestors is an array of post IDs starting with the current post going up the root
//'Pop' the root ancestor out or returns the current ID if the post has no ancestors.
$root_id = (!empty($ancestors) ? array_pop($ancestors): $post_id);
For more info check: http://codex.wordpress.org/Function_Reference/get_post_ancestors
Related Posts:
- Get previously visited page ID
- ID for posts/blogs page
- Get the correct post id for all post and page in header.php
- Get users between user ids
- ID of Front-Page
- Always Encounter Using ID in WordPress as a Final Solution [closed]
- Why does the page/post ID keep growing when i refresh the post-new.php file?
- How can I exclude a specific ID from this line of code?
- get page id’s – not get pages?
- get post by nav id
- Ad units Div-ids frequently changing
- Consequence of the post auto incrementer off?
- Subcategory IDs in relation to parent category ID switch case
- add anchor tags rel to thumbnails id
- get_pages gives wrong ID for blogpage
- Get page ID of page that is set as the posts page
- How to get current page ID outside the loop?
- how to get page id of a page using page slug
- get post author id outside loop
- How to get Author ID outside the loop
- Custom Walker: how to get ID in function start_lvl
- How to get page title with the page ID?
- How to get permalink and title from post ID?
- How do I get the current edit page ID in the admin?
- Echo author ID in author.php
- Get page IDs from nav items
- How to get home page ID?
- Are post ID’s reliable?
- Get current post id in functions.php
- get all posts ID from a category
- How to obtain the user ID of the current profile being edited in WP-Admin?
- Creating a WordPress admin page without a menu for a plugin
- What is an alternative to get_page_by_title()?
- Settings API – creating reusable form elements?
- Get the current post ID as a variable in Javascript
- Is post ID number always incremental n+
- Can’t get post ID in functions.php?
- Get page id by title?
- How to add the category ID to admin page
- Post ID At The End Of Permalink URL Effects Performance (Positively)?
- Importing posts from old website to new conflicting post ID’s?
- Get All IDs Of A Post Type Using WP_Query
- Get user id from email?
- How to change WordPress user ID?
- How to get page’s ID if I know the title only?
- Getting the ID of a meta box
- Is there an equivalent of the PHP function sanitize_key in Gutenberg?
- Are all ID’s used unique?
- get all page IDs from wp_list_pages
- $post->ID displays wrong ID
- Add category to custom post type automatically, using category slug
- At my posts archive page, outside the loop, get_the_id() returns the top most post’s ID
- How to get any tag ID
- get_post_meta fields don’t show up on posts page
- Possible to create a new post and have the Title and Slug automatically use the post’s ID?
- Show ids of displayed media library items
- How do I display Youtube/Vimeo video ID on custom post type when user enters it in custom meta box?
- Showing random content / pictures from earlier posts in a sticky post?
- On Category page, How can I get the category ID?
- Get the post_id of a new post
- Get the comment author ID by the comment ID
- How to get ID of images used in gallery?
- get post id in while loops outputting page id
- How can I display a specific user’s first published post?
- get the_title_attribute by id
- Are Post IDs Unique Across all Post Types?
- Undefined variable post_id in custom quick edit coloumn
- Creating a Front-end based User Search
- Exclude main blog from get_blogs_of_user
- Does the menu item ID ever change?
- Get a post_id where meta_value equals something in a serialized meta_value field
- Return number of items in a table with post id
- How to get user_id from wordpress database inside ajax function?
- Get comment content by comment ID
- Get author ID with attachment ID
- Getting the wrong page ID
- How to retrieve the content (with a specific ID) via ajax by clicking a link tag
- WordPress plugin Write User/staff ID as the same as the WordPress User ID
- get_attachment_id() only get id of first attached image after post update
- Get Post ID with insert/edit link
- URL scheme to retrieve tag archive by ID
- Passing the page ID to a login php script
- get menu id using its name
- How to create a link to jump to “Leave a comment” part?
- How can I get an array of all IDs from the current queried object?
- Getting post id from wp_insert_post_data function?
- How can I get the page url slug when ‘post_name’ returns an id?
- Does an article (post) id ever change?
- Why ids in urls don’t work but slugs do?
- get_users(…) only returns one user
- How to get the post’s parent ID?
- Can a page_id and a post_id be same?
- How can I receive the image id using the media box?
- Fetch ID’s associated with a custom post type when translated with WPML?
- Get post / page ID from ACF Link field
- Cannot get grandparent object
- Make slug as ID Number for custom post types
- Is there a better, more efficient way to get the post id outside the loop?
- post_author for wp_insert_attachment
- How to get category URL with the slug?