这将输出带有最新时间戳的页面标题:
$args = array(
\'post_type\' => array( \'page\' ),
\'order\' => \'DESC\',
\'orderby\' => \'meta_value_num\', // Note was `meta_value` before
\'meta_key\' => \'_cmb_test_datetime_timestamp\',
\'numberposts\' => 1,
);
$posts = get_posts( $args );
if ( $posts ) echo $posts[0]->post_title;
wp_reset_postdata();
要添加条件,请使用
meta_query
条款,例如
$midnight = strtotime( date( \'Y-m-d\', time() + DAY_IN_SECONDS ) );
// If setting timezone when saving data, $midnight += get_option( \'gmt_offset\' ) * HOUR_IN_SECONDS;
$args = array(
\'post_type\' => array( \'page\' ),
\'order\' => \'DESC\',
\'orderby\' => \'meta_value_num\', // Numeric sort.
\'meta_query\' => array(
array(
\'key\' => \'_cmb_test_datetime_timestamp\',
\'compare\' => \'<\',
\'value\' => $midnight,
\'type\' => \'UNSIGNED\', // Note was missing before.
),
),
\'numberposts\' => 1,
);