• Hi everyone!

    I’m having trouble trying to update WordPress core ‘post’ registered arguments to improve a functionality in my website. Specifically, I want to be able to have a hierarchy between my posts so I can set parents and childs for posts. Creating a custom post type to create this won’t help me as all my content is based in posts.

    By default, WordPress allows this only for Pages post type.

    Recently, WordPress added a neat function to modify arguments from an already registered post type, called “register_post_type_args”. This should work both for default and custom post types. I have come accross this code and modified to add what I need: Change capability type of post type registered by plugin

    The results are below. However, when I add this to my functions.php, somehow, I can’t get this to work. My website returns a “Parse error: syntax error, unexpected end of file”.

    add_filter( 'register_post_type_args', 'allow_post_hierarchy' , 10, 2 );
    
    function allow_post_hierarchy( $args, $post_type ){
    
     // Do not filter any other post type
     if ( 'post' !== $post_type ) {
    
         // Give other post_types their original arguments
         return $args;
    
     }
    
     // Change the capability_type of the "custom-css-js" post_type
        $args = array(
            'hierarchical' => 'true',
        );
    
      // Give the custom-css-js post type it's arguments
      return $args;
    
    }

    Does anyone have any idea why this is happening? I appreciate your input. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    There’s nothing wrong with the snippet you posted as far as syntax goes. The end of file error usually is triggered when a parenthesis or brace is missing it’s balancing mate. The first place to look is the last function before yours, you may have accidentally overwritten a curly brace or something when adding your code.

    Any decent programmer’s editor will have a parenthesis/brace/bracket matching feature that will help you verify that each such character has its matching mate. There’s one missing somewhere.

    Note that the error message means PHP was expecting something that didn’t occur before the end of file. It’s invariably a missing curly brace or similar, but it could possibly be something else. As you may know however, the closing ?> of the initial <?php is not required and is generally recommended to not be included at the end of file to prevent unexpected HTML output.

    Besides that, you’re going to find your code does not work the way you want because you are setting the entire arguments array to have only one value. You should only set that one item, leaving the rest of the arguments array unchanged. Also, to set a boolean true, do not include quotes. In some cases it’ll still evaluate to true as a string but it will not evaluate as a true boolean type. Also, 'false' as a string will also evaluate as a boolean true. You have to drop the quotes for it to evaluate as false.

    Finally, I don’t think your code will work in any case because I think the post post_type is established before plugins are evaluated, so your code will have no effect. I’m not sure though, so go ahead and fix your code as suggested and see what happens 🙂

    If that fails, I’d suggest you create a custom hierarchical post type and run an SQL query to convert all of your current posts to the new post type.

    Thread Starter Alan Niemies

    (@alanniemies)

    @bcworkz,

    Thanks a lot for your resourceful input! I ended up creating the new Custom Post Type and moving all my posts to it, as you stated at the end of your answer, as I think this is better than trying to change a core post type.

    All the transition was seamless. After creating my custom post type I used Convert Post Types plugin to move all the posts to there. Had to change some details in my custom queries in homepage but now everything is working as expected, with the added hierarchical capability.

    Thanks a lot again for your invaluable help! You’re awesome. 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Changing default ‘posts’ hierarchy parameter with register_post_type_args’ is closed to new replies.