我正在创建自己的 WordPress 插件,将配置文件导入为帖子,并且在脚本运行时,我希望改进插入/更新帖子的查询。
这个脚本每 5 分钟运行一次,并且像这样工作,
1.从本地用户表中的外部源导入/更新所有配置文件。这很简单并且工作正常
2 . 在更新之前将所有现有帖子自定义字段状态设置为离线
$offline = "UPDATE $meta SET meta_value='offline' WHERE meta_key='status'";
3 . 从表用户中选择所有在线的配置文件
$new_query = "SELECT * FROM users WHERE status='online' ORDER BY visitors DESC";
4 . 对于每个配置文件,如果帖子存在,则更新,否则创建新帖子
$result = $wpdb->get_results($new_query);
foreach ($result as $post){
$cat = $post->gender.'s';
$getterm = get_term_by('name',$cat,'user-category');
$cat_id = $getterm->term_id;
$custfields = array (
'name' => $post->name,
'age' => $post->age,
'subject' => $post->subject,
'status' => $post->status,
'fans' => $post->fans,
'visitors' => $post->visitors,
);
endif;
$postarr = array (
'post_title' => $post->username,
'post_name' => $post->username.'-profile',
'post_content' => 'something',
'post_type' => 'users-profile',
'post_category' => array($cat_id),
'post_status' => 'publish',
'meta_input' => $custfields
);
$postcheck = get_page_by_title($post->username,OBJECT, 'users-profile');
if($postcheck):
update_post_meta($postcheck->ID ,'subject', $post->subject );
update_post_meta($postcheck->ID ,'status', $post->status );
update_post_meta($postcheck->ID ,'visitors', $post->visitors );
else :
wp_set_object_terms(wp_insert_post( $postarr,true),$cat_id,'user-category',true);
endif;
}
)
幕布斯6054654