How to hook page_attributes_meta_box to change displaying “Parent” option?

At WP Admin panel there are “Attributes” metabox with “Parent” dropdown list.
I need to change sort parameters and show only parent posts.
I may do it if I change native WP file meta-boxes.php line 621. Following code:

$dropdown_args = array(
    'post_type'        => $post->post_type,
    'exclude_tree'     => $post->ID,
    'selected'         => $post->post_parent,
    'name'             => 'parent_id',
    'show_option_none' => __('(no parent)'),
    //Remove existing sort
    //'sort_column'      => 'menu_order, post_title',
    'echo'             => 0,
    //Add my options
    'parent'           => 0,
    'sort_order'       => 'DESC',
    'sort_column'      => 'post_date',
);

Using this all work perfect. But i need to hook it. I have created actiont to do it, but it does not works:

Read More
add_action('page_attributes_meta_box', 'custome_page_attributes_meta_box');
function custome_page_attributes_meta_box($post) {
    $post_type_object = get_post_type_object($post->post_type);
    if ( $post_type_object->hierarchical ) {
        $dropdown_args = array(
            'post_type'        => $post->post_type,
            'exclude_tree'     => $post->ID,
            'selected'         => $post->post_parent,
            'name'             => 'parent_id',
            'show_option_none' => '(no parent)',
            'echo'             => 0,
            'sort_order'       => 'DESC',
            'sort_column'      => 'post_date',
            'parent'           => 0
        );

        $dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post );
        $pages = wp_dropdown_pages( $dropdown_args );
        if ( ! empty($pages) ) {
        ?>
            <p><strong><?php _e('Parent') ?></strong></p>
            <label class="screen-reader-text" for="parent_id"><?php _e('Parent') ?></label>
            <?php echo $pages; ?>
        <?php
        } // end empty pages check
    } // end hierarchical check.
}

What I am doing wrong?

Related posts

Leave a Reply

2 comments

  1. You are trying to hook into an inexistant action.
    In the core files, you have to look for do_action and apply_filters.
    In this case, there’s one that’s exactly what you need:

    add_filter( 'page_attributes_dropdown_pages_args', 'filter_dropdown_so_14880043', 10, 2 );
    
    function filter_dropdown_so_14880043( $dropdown_args, $post )
    {
        $my_args = array(
             'post_type'        => $post->post_type,
             'exclude_tree'     => $post->ID,
             'selected'         => $post->post_parent,
             'name'             => 'parent_id',
             'show_option_none' => '(no parent)',
             'echo'             => 0,
             'sort_order'       => 'DESC',
             'sort_column'      => 'post_date',
             'parent'           => 0
        );
        return $my_args;
    }
    
  2. I know this question is old. But I have found a simpler solution to keep all previous arguments and just show private posts:

    add_filter( 'page_attributes_dropdown_pages_args', 'wpse_984638_show_private_pages_in_dropdown', 10, 2 );
    
    function wpse_984638_show_private_pages_in_dropdown( $args, $post ) {
        $args['post_status'] = array('publish', 'private');
        return $args;
    }