[wp-trac] [WordPress Trac] #20647: get_post_type() returns nothing on custom post type archive pages with custom query and no results

WordPress Trac wp-trac at lists.automattic.com
Wed May 9 22:10:13 UTC 2012


#20647: get_post_type() returns nothing on custom post type archive pages with
custom query and no results
-----------------------------+-----------------------------
 Reporter:  MikevHoenselaar  |      Owner:  MikevHoenselaar
     Type:  defect (bug)     |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Post Types       |    Version:  3.3.2
 Severity:  normal           |   Keywords:  needs-patch
-----------------------------+-----------------------------
 As following:
 I open a custom post type archive page. In my case domain.nl/partners.
 Everything works fine here.
 We have a custom filter and use ?bg={number}?country={number} to filter on
 custom field on that archive page.

 We use the pre_get_posts action for that. We set the meta_query query var
 via ->set().

 When we do this and use a {number} in the $_GET that doesn't exists.
 Obviously we have 0 results.
 So $post remains empty in the get_posts() function.

 In a lot of plugins and in this case WordPress SEO of Yoast is a function
 get_post_type() being used. The function relies on $post to function
 properly.

 When there are no results, no $post, no post_type. But I believe we can
 work with the queried_vars here.
 get_query_var( 'post_type' ); to fix it. My solution:

 function get_post_type( $the_post = false ) {
         global $post;
         if ( false === $the_post )
                 $the_post = $post;
         elseif ( is_numeric($the_post) )
                 $the_post = get_post($the_post);

         if ( is_object($the_post) )
                 return $the_post->post_type;
         else
                return get_query_var ('post_type');

         return false;
 }

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/20647>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list