0

I think the title of this post is quite self-describing, but I would like to restrict the list of parent pages (in the edit page form) to only those pages, which are created by logged user - the list should be filtered by Author.

E.g. The user foo has created pages "Animals" and "Vehicles". Now, he would like to create new page "Dogs". He creates new page, fill the title and content, and he would like to change parent page. He clicks on selectbox with the list of parent pages and he will see only "Animals" and "Vehicles". After saving new page "Dogs" he will see "Animals", "Vehicles" and "Dogs".

I think it should be some Wordpress filter (created by the function add_filter()), but I am "novice" in Wordpress, so I am not able to create it now. Could you please help me?

Thank you very much.

1 Answer 1

1

Classic editor

You can use the filter hook page_attributes_dropdown_pages_args and set authors parameter in query arguments.

add_filter( 'page_attributes_dropdown_pages_args', 'se343814_own_post_as_post_parent', 10, 2 );

function se343814_change_post_type_args( $dropdown_args, $post )
{
    //
    // for which types to apply the filter
    if ( $post->post_type != 'page' )
        return $dropdown_args;

    $dropdown_args['authors'] = get_current_user_id();

    return $dropdown_args;
}

Block editor

In the case of a block editor, above solution will not work. The list of posts that can be set as a parent is retrieved by a REST request, with context parameter set to edit. I do not know if it's possible to change the request data when initializing the editor, probably there is no such filter.

However, you can use the rest_{$post_type}_query filter in conjunction with the context that is set in the request.

add_filter( 'rest_page_query', 'se343814_rest_own_post_as_post_parent' , 10, 2 );

function se343814_rest_query_page_parent( $args, $request )
{
    //
    // apply filter only when editing the post
    if ( ! isset($request['context']) || $request['context'] != 'edit' )
        return $args;

    if ( empty($args['author']) )
        $args['author'] = get_current_user_id();

    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.