最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp admin - How to overwrite function to display parent page combobox for custom post type in Edit Post Page?

programmeradmin1浏览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 :

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 ?

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 :

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 ?

Share Improve this question edited May 3, 2020 at 14:17 西門 正 Code Guy - JingCodeGuy 1,5911 gold badge13 silver badges15 bronze badges asked Apr 27, 2020 at 16:10 J.BizMaiJ.BizMai 9002 gold badges10 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论