M.O.A
M.O.A

Reputation: 25

How to retrieve query list of post_type in WordPress?

I am developing a plugin and need the list of post types existing in the WordPress backend, I checked the WP_Query page of WordPress codex but could not find the solution

Upvotes: 0

Views: 39

Answers (1)

Harsh Khare
Harsh Khare

Reputation: 515

You can try this code.

<?php
// hook into init late, so everything is registered
// you can also use get_post_types where ever.  Any time after init is usually 
fine.
add_action( 'init', 'wpse34410_init', 0, 99 );
function wpse34410_init() 
{
$types = get_post_types( [], 'objects' );
foreach ( $types as $type ) {
    if ( isset( $type->rewrite->slug ) ) {
        // you'll probably want to do something else.
        echo $type->rewrite->slug;
    }
}
}

i think it will work for you

Upvotes: 2

Related Questions