0

For some reasons I´m using get_post_ancestors() to display parent pages. In my theme, I wish for my custom post type choose a simple page in the parent page combobox. By default my custom post type whith set hierachical to true will show only post with the same post type in the parent page combobox.

So, I would like to overwrite the function wich list the parent page options for the combobox to add pages in this list.

I mean in the custom post type editor on admin side :

enter image description here

I know there is a way to get parent with a custom metabox like in this tutorial; but I would like avoid doing this and juste change the basic parent combobox.

How can I do this ? Is there a specific hook for that ?

0

Parent Page List

According to your screenshot, it is using the classic editor, you may use get_pages() and get_pages hook to achieve the purpose.

  • The page attribute meta box is created by page_attributes_meta_box() which use wp_dropdown_pages(), wp_dropdown_pages() then use get_pages() to load pages for creating options.

If I understand correctly, you want to add the original post type Page to your custom post type Parent list. The following method could add any other post types to different post types. The example shows is adding Page.

add_filter( 'get_pages', 'ws361150_add_page_to_parent_list', 10, 2 );
function ws361150_add_page_to_parent_list( $post_type_pages, $parsed_args ) {
    // if this is not admin page, return, since get_page is used in also navigation list in frontend

    // avoid unnecessary rendering
    global $current_screen;

    // check to see if it is in edit post type page
    // you may specify the post type $specify_post_type, if add to all custom post types, just leave it blank, if add to certain post types, use preg_match instead of strpos

    $specify_post_type = '';

    if( empty( $current_screen ) || ! empty( $current_screen ) && is_bool( strpos( $current_screen->parent_file, "edit.php?post_type={$specify_post_type}" ) )  ) {
        return $post_type_pages;
    }

    // add Page's page list to current page list or any post types
    $additional_post_type = 'page';
    $found_page = false;

    // *** because it is a filter, need to check if it is already merge, if not doing this, infinite loop will occur
    foreach ($post_type_pages as $key => $page) {
        if( $page->post_type === $additional_post_type ) {
            // var_dump('found');
            $found_page = true;
            break; // no need to continue if found
        }
    }

    if( $found_page ) {
        return $post_type_pages;
    } else {
        // add page list to current post type pages
        $args = array(
            'post_type'        => 'page',
            'exclude_tree'     => $_REQUEST['post'],
        );

        $page_list = get_pages( $args ); // call again get_pages, the above test will return the page list without looping to add if it is already done

        $pages = array_merge( $page_list, $post_type_pages );
        // var_dump('add once');

        return $pages;
    }
}
| improve this answer | |
  • Sorry, maybe my question is not cleared. I would like to do this in admin side. I will change my question. – J.BizMai Apr 28 at 18:00
  • @J.BizMai I have updated the answer. Hopefully, I understand your question correctly. Hope it helps. – simongcc Apr 29 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.