Support » Fixing WordPress » get_post_type() fails in 2.7.1

  • I am using formatting on my search results page that depends on what type of post each item is – to do this I’m using get_post_type(). In 2.7, I was able to use the following (within The Loop):

    if (get_post_type() == "page") do_page_formatting_stuff;

    But after upgrading to 2.7.1, it no longer works. get_post_type() returns the post type of the first post in the query only. Strangely enough, looking through the changelogs indicates that this function hasn’t changed (so I don’t understand why it DID work for me, but doesn’t now). Looking at line 400 of /wordpress/wp-includes/post.php, it says:

    if ( false === $post )
    	$post = $posts[0];

    Why would you want to default to $posts[0]? In a search query, that’s the first post out of the whole lot, right? By changing this to:

    if ( false === $post )
    	$post = $GLOBALS['post'];

    The function works as it should (or at least, how I expect it should). Is this a bug? I don’t understand the logic behind $posts[0], it seems to make no sense in the context of a multi-post query.

  • The topic ‘get_post_type() fails in 2.7.1’ is closed to new replies.