每周运行一次wordpress查询 [英] Run a wordpress query every week
问题描述
早上好。作为标题说,我试图找到一个方法,每周运行一个wordpress查询更新一个部分在我的网站的建议的一周的帖子。
查看我发现了 wp_cron
功能,它看起来像我正在寻找的东西,因为我不能在服务器上设置一个cron作业。
问题是,我不能使它的工作。
我在 functions.php 中创建了这个try函数
add_action 'cron_hook','cron_exec');
if(!wp_next_scheduled('cron_hook')){
wp_schedule_event(time(),'daily','cron_hook');
}
函数cron_exec(){
echo time();
}
我使用以下代码调用我的帖子页面中的操作: p>
do_action('cron_hook');
这是我第一次尝试wp_cron所以也许我错过了一些东西,但我期待得到同样的 time()每次刷新页面,因为函数应该每天触发一次,但是我最后在每次刷新时都有当前的时间戳。
我想知道如果我正确使用wp_cron函数,如果这是正确的事情,或者有bettere方法来实现这个结果。
每周执行cron,你需要这样的东西: function custom_time_cron($ schedule){
$ schedules ['every_week'] = array
'interval'=> 604800,// 604800 seconds in 1 weeks
'display'=> esc_html __('Every Week','textdomain')
);
return $ schedule;
}
add_filter('cron_schedules','custom_time_cron');
add_action('my_cron_hook','my_cron_function');
if(!function_exists('mytheme_my_activation')){
function mytheme_my_activation(){
if(!wp_next_scheduled('my_cron_hook')){
wp_schedule_event (),'every_week','my_cron_hook');
}
}
}
add_action('wp','mytheme_my_activation');
if(!function_exists('my_cron_function')){
function my_cron_function(){
echo time();
}
}
第一个函数创建一个新计划 - 有 houly
,每日
和 twicedaily
作为 reccurance 。
然后设置您的调度程序。您检查是否有!wp_next_scheduled
的计划活动,如果不是,您安排活动
wp_schedule_event(time(),'every_week','my_cron_hook');
如果您已经初始化了一个具有特定名称的cron,需要 wp_clear_scheduled_hook 。
希望此help:)
Good morning everyone. As the title say I'm trying to find a way to run a wordpress query every week to update a section in my site with the suggested posts of the week.
Looking around I've found the wp_cron
feature, and it seems like the thing I'm looking for since I can't set up a cron job on the server.
The problem is that I wasn't able to make it work.
I've created this try-function in my functions.php
add_action( 'cron_hook', 'cron_exec' );
if( !wp_next_scheduled( 'cron_hook' ) ) {
wp_schedule_event( time(), 'daily', 'cron_hook' );
}
function cron_exec() {
echo time();
}
And i call the action in my post page with the following code:
do_action('cron_hook');
This is my first attempt at wp_cron so maybe I missed something but I was expecting to get the same time() everytime I refresh the page since the function should be fired once a day, but I ended up having the current timestamp on every refresh.
I'd like to know if I'm using correctly the wp_cron function and if it's the right thing to use in this case or there are bettere methods to achieve this result.
Thank you for the help and have a nice day.
To execute cron every week you'll need something like this:
function custom_time_cron( $schedules ) {
$schedules['every_week'] = array(
'interval' => 604800, //604800 seconds in 1 week
'display' => esc_html__( 'Every Week', 'textdomain' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'custom_time_cron' );
add_action( 'my_cron_hook', 'my_cron_function' );
if (!function_exists('mytheme_my_activation')) {
function mytheme_my_activation() {
if (!wp_next_scheduled('my_cron_hook')) {
wp_schedule_event( time(), 'every_week', 'my_cron_hook' );
}
}
}
add_action('wp', 'mytheme_my_activation');
if (!function_exists('my_cron_function')) {
function my_cron_function() {
echo time();
}
}
The first function creates a new schedule - because you only have houly
, daily
and twicedaily
as a reccurance.
Then you set up your scheduler. You check if there is a scheduled event with !wp_next_scheduled
, and if it's not, you schedule the event
wp_schedule_event( time(), 'every_week', 'my_cron_hook' );
If you've already initialized a cron with a certain name, for it to get rescheduled you'll need to wp_clear_scheduled_hook.
Hope this helps :)
这篇关于每周运行一次wordpress查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!