你好Wordpress社区,
我想为自己构建一个“每日食谱”功能。
我正在寻找一种方法,通过插件或Cronjob将每天3种自定义帖子类型标记为“特色帖子”。
第二天,3个新帖子将被标记为“特色”。
有没有一种简单的方法可以做到这一点,或者有没有一种快速简单的方法可以通过函数实现这一点。子主题中的php?
已经非常感谢了
你好Wordpress社区,
我想为自己构建一个“每日食谱”功能。
我正在寻找一种方法,通过插件或Cronjob将每天3种自定义帖子类型标记为“特色帖子”。
第二天,3个新帖子将被标记为“特色”。
有没有一种简单的方法可以做到这一点,或者有没有一种快速简单的方法可以通过函数实现这一点。子主题中的php?
已经非常感谢了
好的,首先您需要添加自己的WP\\U计划事件:
add_action( \'wp\', function () {
if (! wp_next_scheduled ( \'mark_posts_as_featured_event\' )) {
wp_schedule_event(time(), \'daily\', \'mark_posts_as_featured_event\');
}
} );
function mark_posts_as_featured_event_callback() {
// if there are sticky posts in our CPT, unstick them
$sticked_post_ids = get_option( \'sticky_posts\' );
if ( ! empty ) {
$old_featured_posts = get_posts( array(
\'post_type\' => \'<MY_POST_TYPE>\',
\'fields\' => \'ids\',
\'post__in\' => $sticked_post_ids,
) );
foreach ( $old_featured_post_ids as $post_id ) {
unstick_post( $post_id );
}
}
// stick new posts
// get_random_posts
$new_featured_post_ids = get_posts( array(
\'post_type\' => \'<MY_POST_TYPE>\',
\'posts_per_page\' => 3,
\'orderby\' => \'rand\',
\'fields\' => \'ids\',
) );
foreach ( $new_featured_post_ids as $post_id ) {
stick_post( $post_id );
}
}
add_action( \'mark_posts_as_featured_event\', \'mark_posts_as_featured_event_callback\' );
在我的wp-config.php, 我有define(\'DISABLE_WP_CRON\', true); 我在Mac(Catalina)上,如果我从终端运行crontab - l 我明白了* * * * * wget -q -O - \'https://mysiite.com/wp-cron.php?doing_wp_cron&cron=true\'; echo "wordpresscron" >> /somepath/stdout.log