0

I have looked all over the Codex and searched the web and I cannot find a way to accomplish what I am trying to do. All I need to do is display what CPT a post belongs to. For instance, I have two different post types, Courses and Resources, and I need a way to output which one of these a particular post belongs to. Can anyone help?

2 Answers 2

2

get_post_type() should do what you're asking.

1
  • 1
    Wow... I should either sleep or quit my job. Thanks!!!
    – dericcain
    Nov 25, 2014 at 19:10
2

You can also directly access the WP_Post objects inside the loop. The current post post_type can be displayed with

echo $post->post_type;

EDIT

After some constructive comments to this answer (thank you @ialocin), I felt that it would really be beneficial to extend to my answer and shed some more light on this issue for future reference

You need to first look at the source code of get_post_type()

1121    /**
1122     * Retrieve the post type of the current post or of a given post.
1123     *
1124     * @since 2.1.0
1125     *
1126     * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
1127     * @return string|bool Post type on success, false on failure.
1128     */
1129    function get_post_type( $post = null ) {
1130            if ( $post = get_post( $post ) )
1131                    return $post->post_type;
1132    
1133            return false;
1134    }

As one can see from the source of get_post_type(), it is simply just a wrapper for $post->post_type. There is absolutely no impact on time or DB calls from the use of any of the two methods. As @PatJ stated in a comment, 6 of one and half dozen of the other.

The advantage however from using get_post_type() is that you can get the post type from any post outside the loop by passing any post ID to it. In this case it will use get_post to retrieve the post type from the post which is given. This is also very efficient and not resource intensive.

Here is the test result from that to state my point. My post ID is 159

echo get_post_type(159);

gives a test result of

1 queries in 0.00098 seconds.

8
  • 1
    Which, getting the post type from a post type object, pretty much is what get_post_type() does. Nov 25, 2014 at 19:30
  • 2
    That works too. The only advantage to get_post_type(), I suppose, is that you can use it outside The Loop. But the OP's question seems to take place inside The Loop, so in this case it's six of one.
    – Pat J
    Nov 26, 2014 at 2:56
  • @ialocin jip. It is like Pat also said 6 of one and a half dozen of the other. I always use the WP_Post objects like ID and post_type inside the loop directly. Personal preference :-) Nov 26, 2014 at 4:00
  • 1
    @ialocin You have a valid point there. I have updated my post accordingly. Sometimes we take such extras for granted , not thinking about others that might not know this much as we do. And you are correct in stating that this will come up again someday. Nov 26, 2014 at 17:04
  • 1
    I would give you another upvote for the effort, but of course I can't. And, yes, it will come up and we always forget. Nov 26, 2014 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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