【问题标题】:Update all posts in custom post type with wp_cron()使用 wp_cron() 更新自定义帖子类型的所有帖子
【发布时间】:2018-04-13 15:46:27
【问题描述】:

如果帖子元值设置为 true,我已经创建了一个函数来将分类术语应用于帖子。这可以正常工作。
我面临的问题是它仅在我手动保存/更新帖子后才会更新。 有没有办法为自定义帖子类型内的所有帖子安排或动态执行此操作?

我的分类术语函数代码:-

function save_cp_term_meta( $post_id, $post, $update ) {
    $termshouldbe='new';

    $meta_value = get_post_meta( $post->ID, 'new_used_cat', true ); 
        if  (!empty( $meta_value )) 
        { 
           $termshouldbe='used';
        }
        else 
        {
        } 

    wp_set_object_terms($post_id,$termshouldbe,'vehicle_condition',false);
}
add_action( 'save_post', 'save_cp_term_meta', 10, 3 );

【问题讨论】:

  • 嘿,Shaun,这需要定期运行(cron 是一个不错的选择)还是只需要运行一次,所有以前的帖子都会更新?
  • 我每天下午 5 点有一次导入,所以最好每天进行
  • 我发布了一个答案,我认为这是满足您需求的可能解决方案,如果有任何不清楚的地方请我澄清。

标签: php database wordpress cron


【解决方案1】:

您可以使用WP_Cron() 安排任务在每天的特定时间运行并执行更新。

// check if the next cron is ours, if not schedule it.
if ( ! wp_next_scheduled( 'prefix_save_cp_term_meta_cron' ) ) {
    wp_schedule_event( strtotime( '5:15PM' ), 'daily', 'prefix_save_cp_term_meta_cron' );
}
/**
 * function to query for posts of a certain post type and update
 * some term_meta based on a post_meta value.
 */
function prefix_save_cp_term_meta_runner() {
    // make sure to set the correct post type you want here.
    $args = array(
        'post_type'      => array( 'your post type' ),
        'meta_key'       => 'new_used_cat',
        'posts_per_page' => -1,
    );

    // run the query.
    $query = new WP_Query( $args );

    // if the query has posts...
    if ( $query->have_posts() ) {
        // loop through them...
        while ( $query->have_posts() ) {
            $query->the_post();
            // default value to use as term.
            $termshouldbe = 'new';
            // get the current post_meta if it exists.
            $meta_value = get_post_meta( $post->ID, 'new_used_cat', true );
            if ( ! empty( $meta_value ) ) {
                // update the value for term based on having a value for new_used_cat.
                $termshouldbe = 'used';
                // NOTE: you may want to delete the post_meta here so that next
                // iteration this query doesn't need to work with this post
                // again as it's already processed.
            }
            // set the term with a value (either 'new' or 'used').
            wp_set_object_terms( $post->ID, $termshouldbe, 'vehicle_condition', false );
        }
        // restore the main query.
        wp_reset_postdata();
    }
} // End if().
add_action( 'prefix_save_cp_term_meta_cron', 'save_cp_term_meta_runner' );

注意:您可能希望每隔一段时间而不是在固定时间添加它 - 或者改为通过系统级 cron 作业运行它。此答案详细说明了使用 WP_Cron() 时可能会帮助您确定哪种方法最适合您的一些问题:https://wordpress.stackexchange.com/a/179774/37158

【讨论】:

  • 嗨威廉。感谢您的回答,我很感激。当我粘贴到我的函数中时,它会破坏网站吗?有什么想法吗?
  • 嘿,抱歉,我在从测试站点拉出代码后确实调整了代码,所以我可能添加了一个错字或其他东西:( 让我稍后再看一下,当我修复它时我会 ping 你.
  • 不用担心威廉,感谢您的帮助!
  • @ShaunPhillips 嘿,对不起,我昨天没讲到这个。我确实在代码中遇到了问题 - 一个额外的括号。现在应该修好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 2015-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多