Using WordPress ‘page_attributes_dropdown_pages_args’ PHP filter

The page_attributes_dropdown_pages_args WordPress PHP filter allows you to modify the arguments used to generate a Pages drop-down element.

Usage

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

function customize_dropdown_pages_args($dropdown_args, $post) {
  // your custom code here
  return $dropdown_args;
}

Parameters

  • $dropdown_args (array): Array of arguments used to generate the pages drop-down.
  • $post (WP_Post): The current post.

More information

See WordPress Developer Resources: page_attributes_dropdown_pages_args

Examples

Exclude specific pages from the drop-down

This example modifies the $dropdown_args to exclude specific pages from the drop-down list by their IDs.

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

function exclude_specific_pages($dropdown_args, $post) {
  $exclude_ids = array(5, 15, 25); // The IDs of the pages to exclude
  $dropdown_args['exclude'] = implode(',', $exclude_ids);
  return $dropdown_args;
}

Display only top-level pages

This example updates the $dropdown_args to display only top-level pages in the drop-down list.

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

function show_top_level_pages_only($dropdown_args, $post) {
  $dropdown_args['depth'] = 1;
  return $dropdown_args;
}

Sort pages by title in ascending order

This example changes the $dropdown_args to sort the pages by their title in ascending order.

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

function sort_pages_by_title($dropdown_args, $post) {
  $dropdown_args['sort_column'] = 'post_title';
  $dropdown_args['sort_order'] = 'ASC';
  return $dropdown_args;
}

Display only published pages

This example modifies the $dropdown_args to display only published pages in the drop-down list.

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

function show_published_pages_only($dropdown_args, $post) {
  $dropdown_args['post_status'] = 'publish';
  return $dropdown_args;
}

Change the drop-down element’s name attribute

This example updates the $dropdown_args to change the drop-down element’s name attribute.

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

function change_dropdown_name_attribute($dropdown_args, $post) {
  $dropdown_args['name'] = 'custom_page_attribute';
  return $dropdown_args;
}