Custom post type pages – get the parent pages only?

I have a custom post page set below:

function keyword_tiles_init() {
    $args = array(
      'label' => 'Keywords',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'page',
        'hierarchical' => true,
        'rewrite' => array('slug' => 'keywords'),
        'query_var' => true,
        'menu_icon' => 'dashicons-admin-page',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'parent',
            // 'trackbacks',
            //'custom-fields',
            //'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',
            )
        );
    register_post_type( 'keyword-tile', $args );
}
add_action( 'init', 'keyword_tiles_init' );

It has parent and child pages. And I want to query the parent pages only:

$page_slug = 'keywords';
$slug_type = 'keyword-tile';
$output_type = ARRAY_A;
$page = get_page_by_path($page_slug, $output_type, $slug_type);
var_dump($page);

Result:

NULL

I have set the slug:

'rewrite' => array('slug' => 'keywords'),

So the param to pass in get_page_by_path should be:

$page_slug = 'keywords';

But it does not work.

Any ideas?

Read more here: Custom post type pages – get the parent pages only?

Leave a Reply

Your email address will not be published. Required fields are marked *