post type - How to get all CPT names into WP_Query 'post_type' parameter?

helloI want to display post based on his ID. This post could be a default 'post', 'page' or other re

hello

I want to display post based on his ID. This post could be a default 'post', 'page' or other registered custom post type. In query I sould specify all CPT names which I want to use, for eg.

 'post_type' => array('post', 'page', 'my_cpt')

My question is how can I set 'post_types' automatically for all registered post types, without manually specify them?

I'm striving for something like that:

$post_types = ALL-POST-TYPES-NAMES;
$the_query = new WP_Query(
  array(
    'post_type' => $post_types,
    'p' => ID,
  )
);

Thanks!

hello

I want to display post based on his ID. This post could be a default 'post', 'page' or other registered custom post type. In query I sould specify all CPT names which I want to use, for eg.

 'post_type' => array('post', 'page', 'my_cpt')

My question is how can I set 'post_types' automatically for all registered post types, without manually specify them?

I'm striving for something like that:

$post_types = ALL-POST-TYPES-NAMES;
$the_query = new WP_Query(
  array(
    'post_type' => $post_types,
    'p' => ID,
  )
);

Thanks!

Share Improve this question asked Jul 1, 2019 at 16:50 D_PD_P 1531 gold badge3 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can get a list of post types with the get_post_types() function:

$post_types = get_post_types();

In your case you'll want to set the second $output parameter to names (since that's what you need to pass to the post_type argument), and you'll probably want to set the $args in the first argument so that only public post types are returned, otherwise you could end up with weird stuff like menu items and revisions:

$post_types = get_post_types( [ 'public' => true ], 'names' );

However, it looks like you're just looking for a specific post based on the ID. If that's the case, then you don't need post types or a query at all. Just pass the ID to get_post():

$post = get_post( $id );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745353818a4623999.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信