I'm working on a WordPress site and I use the following code to show posts in two columns on archive and category pages:

<div id="post-<?php the_ID(); ?>" <?php post_class( 0 === ++$GLOBALS['wpdb']->current_post % 2 ? 'grid col-340 fit' : 'grid col-340' ); ?>>

With debug set to "true" I got the following notice:

Notice: Undefined property: wpdb::$current_post in D:\beta\www.beta.dev\wp-includes\wp-db.php on line 684

What can be wrong with this code? I noticed that this notice appeared with WordPress version 4+

Any help will be greatly appreciated. Thanks.

2 Answers 2


There are problems with this part:


There's no current_post property of the wpdb class, here you're most likely confusing the wpdb class with the WP_Query class.

Also we wouldn't want in general to modify the current_post property of the global WP_Query, it could surely "bite us" if we mess with the globals ;-)

Note that the current_post property of WP_Query is already doing the counting for us with $this->current_post++; inside next_post(), that's called within the_post(). See here. So there's no need to increase it manually (++) within the loop.

Here's an example using the post_class filter, with the help of a static variable:

add_filter( 'post_class', function( $classes ) 
    static $instance = 0;

    if( in_the_loop() )
        $classes[] = ( 0 === $instance++ % 2 ) ? 'even' : 'odd';        

    return $classes;
} );

where we target post classes in the main query loop. Just remember to modify the even/odd classes and other restrictions to your needs.

Using the post_class filter could mean better re-usability for our template parts.


It looks like you're using the first version of @toscho's one-liner answer, creating a custom current_post property (for counting) of the global wpdb object. He suggested then to use the prefixed custom property, like wpse_post_counter. But it looks like it needs an initialization to avoid the PHP notice.

@kaiser did post a great answer here that uses the current_post property of the global $wp_query (probably not the global $wpdb).

Since I gave a promise here, regarding anonymous functions and global variables, I should rewrite it to: See my edit here - where we use the use keyword to pass on the global $wp_query object.

  • Here's a post where I found the same method that I use. However, I'm a bit confused. What is the corect code in order to avoid that notice? Thanks.
    – Knott
    Mar 1, 2016 at 11:58
  • 1
    @knott ps: the workaround trial suggestion was 0 === $GLOBALS['wp_query']->current_post % 2 , i.e. without modifying any globals with ++ (looks like you're in the main query loop), but I would recommend the post_class filter.
    – birgire
    Mar 1, 2016 at 13:53
  • 1
    @birgire I actually meant to use global $wpdb and $wp_query->current_post inside your if ( in_the_loop() ) condition. Also, you can assign arbitrary properties to objects like $GLOBALS['wpdb']->wpse219381_foo without a problem. Prefixing them should be enough. Also, I don't have a dev setup for WP at the moment, so I am not sure if current_post is a property of wp_query or wpdb. I assume the former has it.
    – kaiser
    Mar 1, 2016 at 14:32
  • 1
    thanks @kaiser for the clarification: the current_post is a property of WP_Query, I just thought I was misunderstanding (wouldn't be the first or last time ;-) that it should also be part of part of wpdb. Also there was no initialization before ++, so it triggers a PHP notice. Yes I sometimes like to create custom properties of e.g. the WP_Query object, to simplify life.
    – birgire
    Mar 1, 2016 at 16:04
  • 2
    @kaiser This makes sense to me now as well. Thanks for explaining. birgire: I was just in the dark as you. +another1 for your update. I think that would be really helpful to many other people. Cleverly abusing globals could be a fun new way to explore, although I still do not really buy into this yet, still throwing caution to the wind ;-) Mar 1, 2016 at 16:13

use get_the_ID() to get the current post id

<?php echo get_the_ID(); ?>

and this is how you call Global variable in wp global $wpdb;;

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.