Question

I am trying to build a customer portal however the private page is not showing in the parent drop-down (The drop-down at the right bottom of page settings under page attributes). Both the parent and child pages need to be private so that only logged in users can access it.

I have tried this code which didn't work for me.

/**
 * Add private/draft/future/pending pages to parent dropdown.
 */
function wps_dropdown_pages_args_add_parents( $dropdown_args, $post = NULL ) {
    $dropdown_args['post_status'] = array( 'publish', 'private', );
    return $dropdown_args;
}

add_filter( 'page_attributes_dropdown_pages_args', 'wps_dropdown_pages_args_add_parents' );
add_filter( 'quick_edit_dropdown_pages_args', 'wps_dropdown_pages_args_add_parents' );
Était-ce utile?

La solution

The following conditions is assumed to make it working

  • Classic Editor is in use, since Gutenberg/Block Editor is rendered by Javascript, it requires another solutions and so far I am not sure if there is override option yet.

For the code you have shown, it does not work because the number of arguments for add_filter() is not specified. By default, only 1 argument is used. To use more than 1 argument, it is necessary to specify.

Code correction (The asker's code is for Classic Editor)

The following is tested, it works for any post types even custom post types. The following code is proved to work by putting in theme's functions.php

/**
 * Add private/draft/future/pending pages to parent dropdown.
 */
function wps_dropdown_pages_args_add_parents( $dropdown_args, $post = NULL ) {
    $dropdown_args['post_status'] = array( 'publish', 'private', );
    return $dropdown_args;
}

add_filter( 'page_attributes_dropdown_pages_args', 'wps_dropdown_pages_args_add_parents', 10, 2 );
add_filter( 'quick_edit_dropdown_pages_args', 'wps_dropdown_pages_args_add_parents' );

Disable the Block Editor (for Classic Editor's method)

In case anyone would like to disable the Gutenberg editor for testing the above code, you may put the following code in theme's functions.php

add_filter( 'use_block_editor_for_post_type', 'sing_use_block_editor_for_post_type', 10, 2 );
function sing_use_block_editor_for_post_type( $can_use, $post_type ) {
    switch ( $post_type ) {
        case 'custom_post_types':
        case 'post':
        case 'page':
            $can_use = false;
            break;

        default:
            $can_use = true;
            break;
    }

    return $can_use;
}

Override method for Gutenberg/Block Editor

Please use the filter rest_{$this->post_type}_query to override the query

Because Gutenberg is using Rest API to load data, page list is one of them. The following is tested on the same test pages with Parent and Child both private. The following code is proved to work by placing in theme's functions.php

add_filter( 'rest_page_query', 'test_rest_page_query', 10, 2);
function test_rest_page_query( $args, $request ) {
    // please add your own logic such as screen id check logic

    $args['post_status'] = array( 'publish', 'private' );
    return $args;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top