【问题标题】:How do I delete a post through a meta_value wordpress如何通过 meta_value wordpress 删除帖子
【发布时间】:2020-02-25 12:53:05
【问题描述】:

当我在 Wordpress 中创建帖子时,我通过一个名为 Post Expirator 的插件来确定过期日期。我的问题是如何在它们过期时从主页、类别等中删除这些帖子。

另外,我会让所有过期的帖子都显示在我网站的某个位置。

我尝试过使用 meta_keys 和 meta_value ,但没​​有成功。

<?php $args = array(
            'meta_key' => '_expiration-date',
            );
$query = new WP_Query( $args ); ?>

<?php if($query->have_posts()) : ?>
   <?php while($query->have_posts()) : $query->the_post() ?>

         <?php get_template_part( 'content', get_post_format() ); ?>

   <?php endwhile ?>
<?php endif ?>

使用上面的代码,我可以显示我添加过期日期的帖子,无论它过期的日期是什么,现在想知道如何通过将过期日期与当前日期进行比较来循环删除它们。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    试试这个,我的网站上有A Code Work,首先你的_expiration-date键值格式是Y-m-d表示2015-10-17必须

    <?php
    $today = date("Y-m-d");
    $args = array(
        'posts_per_page'   => -1,       
        'orderby'          => 'date',
        'order'            => 'DESC',       
        'meta_key'         => 'clx_event_start_date',
        'meta_query'  => array(
             array(         // restrict posts based on meta values
              'key'     => '_expiration-date',  // which meta to query
              'value'   => $today,  // value for comparison
              'compare' => '>='   // method of comparison              
         )
        'post_type'        => 'post', //your post type
        'post_status'      => 'publish'     
    );
    $posts_array = get_posts( $args );
    
    foreach($posts_array as $RowCron){      
        echo $post_id = $RowCron->ID; 
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      相关资源
      最近更新 更多