在PHP中访问对象值

时间:2018-02-05 15:18:18

标签: php wordpress object

我想在WordPress中访问此对象的日期值, 但我无法得到任何结果。

 $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
    )

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您可以更改" YEAR(post_date)"的名称,尝试:

Inception

您可以拨打:SELECT DISTINCT YEAR(post_date) as pDate FROM wp_posts ORDER BY post_date

注意:我在查询中添加了$date->pDate;

答案 1 :(得分:1)

试试这个

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

或只是更改查询中的名称