Modify built-in post type properties

I have an unusual situation where I need to make the built-in post type 'page' non-hierarchical.

I printed the post type object with var_dump(get_post_type_object('page')); die; and I got this:

object(stdClass)#164 (26) {
  ["labels"]=
  ...
  }
  ["description"]=
  string(0) ""
  ["publicly_queryable"]=
  bool(false)
  ["exclude_from_search"]=
  bool(false)
  ["capability_type"]=
  string(4) "page"
  ["map_meta_cap"]=
  bool(true)
  ["_builtin"]=
  bool(true)
  ["_edit_link"]=
  string(16) "post.php?post=%d"
  ["hierarchical"]=
  bool(true)
  ....
}

How might I go about modifying the post-type object so that ["hierarchical"]=bool(false)?

Topic oop admin pages Wordpress

Category Web


What about:

function wp1482371_page_post_type_args( $args, $post_type ) {

    if ( $post_type == "pages" ) {
        $args['hierarchical'] = false
        );
    }

    return $args;
}
add_filter( 'register_post_type_args', 'wp1482371_page_post_type_args', 20, 2 );

This is a pretty late answer, but I was looking to do something similar and figured it out. I wanted to get nav_menu_items into an RSS feed, which required changing the built in nav_menu_item post type property publicly_queryable.

Anyway, it was actually pretty simple, here's a generic function to do it:

function change_wp_object() {
  $object = get_post_type_object('post_type');
  $object->property = true;
}
add_action('init','change_wp_object');

And that's it. I've got it in a plugin. If you want to see the list of available properties to change, throw in

echo '<pre>'.print_r($object, 1).'</pre>';

to get a nicely formatted output of all the properties.

In your case you'd use

$object-> hierarchical = false;

Hope that helps someone!


As far as I can tell, there is no (good) way to change the core post types. The alternative of course is to make your own custom post type. If you have a bunch of posts that are the core post type of page, you could run a MySQL query to convert them all at once to the new post type, once created, if you need to. Or, you could possibly change the way your theme displays a list of pages.

To make a new post type that is not hierarchical:

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'nonhierarchical_page',
        array(
            'labels' => array(
                'name' => __( 'NHPages' ),
                'singular_name' => __( 'NHPage' )
            ),
            'public' => true,
            'hierarchical' => false
        )
    );
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.