2

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' );
2
  • what is your code to retrieve the dropdown list? Apr 30, 2020 at 3:43
  • @simongcc I have not added any code from my end I did try one. I have edited the answer with the same. The parent drop-down gets populated itself when we create/edit a page (The dropdown at the right bottom of page settings under page attributes) Apr 30, 2020 at 3:59

2 Answers 2

4

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;
}
8
  • It's not working. I am still not getting the private page as option. This function needs to be added in function.php file right? Apr 30, 2020 at 4:59
  • Yes, I just added to the functions.php and it works immediately. I have created a private page and assign it to a child. BTW, this method is only working for classic editor. For Gutenberg/Block Editor, it requires different solutions. Are you using Gutenberg editor? If so, you could disable the Gutenberg editor to test. Apr 30, 2020 at 6:00
  • is your child page also private? Yes I am using Gutenberg editor. Ok so other than changing the editor there is no solution? Apr 30, 2020 at 6:48
  • Yes. The child page is also private. I am trying and looking into the source code to see if there is any chance to get it done. Apr 30, 2020 at 9:45
  • @ArushiRajput I have added the solutions for Gutenberg and proved to work with a test using same private pages setup. Apr 30, 2020 at 11:17
0

A little security and scope tweak to @arushi-rajput 's snippet:

/**
 * Show all parents, regardless of post status. (for Gutenburg Editor)
 */

add_filter( 'rest_page_query', 'test_rest_page_query', 10, 2);
function test_rest_page_query( $args, $request ) {
    // Check if user has the 'edit_pages' capability
    if ( current_user_can( 'edit_pages' ) ) {
        $args['post_status'] = array( 'publish', 'private', 'draft', 'pending', 'future' );
    } else {
        // For other users, only show 'publish' status
        $args['post_status'] = array( 'publish' );
    }
    return $args;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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