我有一个帖子过期的网站。目前我手动执行此操作,但我更喜欢自动执行此操作。几个月来,我一直在努力解决这个问题,我想我终于用wp\\u query()找到了解决方案,但它并没有显示预期的结果,而是显示了最近发布的10篇文章。
这是当前代码:
function expire_posts() {
$post_id = get_the_ID();
$getdate = get_post_meta($post_id, \'Expiry Date\', true);
$expirydate = date (\'d F Y\', strtotime ($getdate));
$today = date( \'d F Y\');
$args = array (strtotime($expirydate) < strtotime($today));
$query = new WP_Query($args);
?><ol><?php
if( $query->have_posts() ) : while ( $query->have_posts() ) : $query-
>the_post();
?>
<li><?php the_title(); ?></li><?php
endwhile;
?></ol><?php
wp_reset_postdata();
endif;
}
我还使用了以下代码,它返回了相同的结果:function expire_posts() {
$args = array(
\'meta_key\' => \'Expiry Date\',
\'meta_value\' => date( "d F Y" ), // change to how "event date" is stored
\'meta_compare\' => \'<\',
);
$query = new WP_Query( $args );
?><ol><?php
if( $query->have_posts() ) : while ( $query->have_posts() ) : $query-
>the_post();
?>
<li><?php the_title(); ?></li><?php
endwhile;
?></ol><?php
wp_reset_postdata();
endif;
}
如果过期,它应该返回两个特定的已发布帖子。