4

I am trying to get Pages, Posts and Custom Post Types In WordPress website but it is showing some other things in the list.

My Code:

<?php $types = get_post_types( ['public'   => true ], 'objects' );
         foreach ( $types as $type ) {
    
            if ( isset( $type->labels->name) ) { ?>
           
           <?php echo $type->labels->name ?>
           <br>
           
           <?php }
} ?>

In this, I am getting:

Posts
Pages
Media
My Templates
Locations

But I only want Pages, Posts and Locations (Locations is the Custom Post Type).

Any help is much appreciated.

4
  • Media is the attachments, the media library isn't a folder/file viewer, each uploaded file has a post of type attachment in the database. I don't know what My Templates is though, knowing more information about each post type would help, but there's a good chance that it's being added by a 3rd party plugin
    – Tom J Nowell
    Jan 8 at 14:24
  • @TomJNowell, How I can remove that. I have added Elementor, so that's why My Templates is showing. Jan 8 at 14:59
  • @TomJNowell, I only want to show the Pages, Posts & All The Custom Post Types. Jan 8 at 15:00
  • My Templates is a custom post type, if you want just the post types that you registered and no others, you need to whitelist them with an array that lists the post types you registered
    – Tom J Nowell
    Jan 8 at 15:29
5

You can make an array of the post types you don't want and then check in_array() to see if they match before you output anything with them.

<?php
    //You'll want to get at the actual name for My Templates.
    //My attempt is just a guess.
    $types_array = array( 'attachment' , 'elementor_library' );
    $types = get_post_types( ['public'   => true ], 'objects' );
        foreach ( $types as $type ) {
            if( !in_array( $type->name, $types_array )) {
                if ( isset( $type->labels->name) ) {
                    echo $type->labels->name . '<br>';
                }     
            }
        }
?>
9
  • In this thing, How you will get all the custom post types that exists in the WordPress. I am trying to get the Pages, Posts And All the Custom Post Types. Locations is Custom Post Types. If someone has movies as a Post Type then? Jan 8 at 16:00
  • Ahhhh, I misunderstood... For some reason I got it in my head that you knew specifically what you needed already. Jan 8 at 16:02
  • How to get the solution for this? Jan 8 at 16:27
  • I have an idea, just in the middle of something for work though... Jan 8 at 16:31
  • I did some digging in the codex and there doesn't appear to be a way to exclude types directly in get_post_types() so instead we'll just exclude them during the output. I don't know what the Elementor post_type really is so I guessed. I'm pretty sure I guessed wrong though. Jan 8 at 16:42
1

I tried this and got the solution:

<?php
 $types = get_post_types( ['public'   => true ], 'objects' );
 $exclude = array( 'attachment' , 'elementor_library' );

    foreach ( $types as $type ) {
      if( !in_array( $type->name, $exclude )) {
            if ( isset( $type->labels->name) ) {
                echo $type->labels->name . '<br>';
            }     
        }
    }
?>

You are use this or the above one also.

In this, You will get:

Pages
Posts
Custom Post Types

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.