考虑WordPress表前缀

时间:2010-10-13 作者:curtismchale

给定下面的代码,我如何解释可以在不同安装之间更改的表前缀?有没有比SQL查询更好的编写方法?是否有一个我遗漏的变量为这样的时间提供表前缀?

// setting posts with current date or older to draft
if (!wp_next_scheduled(\'sfn_expire_hook\')){
    wp_schedule_event( time(), \'hourly\', \'sfn_expire_hook\');
}
add_action( \'sfn_expire_hook\', \'sfn_show_expire\');
function sfn_show_expire(){
    global $wpdb;
    $server_time = date(\'mdy\');
    $result = $wpdb->get_results("SELECT * FROM GrandDn4wP_posts WHERE post_type = \'show\' AND post_status = \'publish\'");
    if( !empty($result)) foreach ($result as $a){
        $show_time = get_the_time(\'mdy\', $a->ID);
        if ( $server_time > $show_time ){
           $my_post = array();
           $my_post[\'ID\'] = $a->ID;
           $my_post[\'post_status\'] = \'draft\';
           wp_update_post( $my_post );
        }
    } // end foreach
}

1 个回复
最合适的回答,由SO网友:sorich87 整理而成

您可以这样重写代码:

// setting posts with current date or older to draft
if (!wp_next_scheduled(\'sfn_expire_hook\')){
    wp_schedule_event( time(), \'hourly\', \'sfn_expire_hook\');
}
add_action( \'sfn_expire_hook\', \'sfn_show_expire\');
function sfn_show_expire(){
    global $wpdb;
    $server_time = date(\'mdy\');
    $result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = \'show\' AND post_status = \'publish\'");
    if( !empty($result)) foreach ($result as $a){
        $show_time = get_the_time(\'mdy\', $a->ID);
        if ( $server_time > $show_time ){
           $my_post = array();
           $my_post[\'ID\'] = $a->ID;
           $my_post[\'post_status\'] = \'draft\';
           wp_update_post( $my_post );
        }
    } // end foreach
}
但更好的编写方法是使用函数get\\u posts:

// setting posts with current date or older to draft
if (!wp_next_scheduled(\'sfn_expire_hook\')){
    wp_schedule_event( time(), \'hourly\', \'sfn_expire_hook\');
}
add_action( \'sfn_expire_hook\', \'sfn_show_expire\');
function sfn_show_expire(){
    $server_time = date(\'mdy\');
    $result = get_posts( array( post_type => \'show\', post_status => \'publish\' ) );
    if( !empty($result)) foreach ($result as $a){
        $show_time = get_the_time(\'mdy\', $a->ID);
        if ( $server_time > $show_time ){
           $my_post = array();
           $my_post[\'ID\'] = $a->ID;
           $my_post[\'post_status\'] = \'draft\';
           wp_update_post( $my_post );
        }
    } // end foreach
}

结束

相关推荐

How do you debug plugins?

我对插件创作还很陌生,调试也很困难。我用了很多echo,它又脏又丑。我确信有更好的方法可以做到这一点,也许是一个带有调试器的IDE,我可以在其中运行整个站点,包括插件?