de albuquerque frank
de albuquerque frank

Reputation: 197

Access object value in PHP

I would like to access the date value from this object in WordPress, but I can't get any results.

 $dates = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) FROM wp_posts 
                              ORDER BY post_date");
     foreach($dates as $date){
             echo $date->YEAR(post_date);
            }



 stdClass Object
    (
        [YEAR(post_date)] => 2010
    )
    stdClass Object
    (
        [YEAR(post_date)] => 2011
    )
    stdClass Object
    (
        [YEAR(post_date)] => 2012
    )
    stdClass Object
    (
        [YEAR(post_date)] => 2013
    )

How can I fix this?

Upvotes: 0

Views: 56

Answers (2)

Arun Kumaresh
Arun Kumaresh

Reputation: 6311

try this

print_r($date->{'YEAR(post_date)'});

or simply change the name in the query

Upvotes: 1

Scriptman
Scriptman

Reputation: 432

You can change the name of "YEAR(post_date)", try:

SELECT DISTINCT YEAR(post_date) as pDate FROM wp_posts ORDER BY post_date

You can call with: $date->pDate;

Note: I added as pDate to the query

Upvotes: 1

Related Questions