Wordpress-连接到保存post数据
如果post自定义字段设置为true,则这段代码应设置post数据;如果未设置为true,则应将post状态设置为draft。该操作需要在Wordpress-连接到保存post数据,wordpress,Wordpress,如果post自定义字段设置为true,则这段代码应设置post数据;如果未设置为true,则应将post状态设置为draft。该操作需要在post\u save/post\u updated操作后调用,因此我们的一所大学向我建议了方法(旧问题:): 它做了它应该做的事情——它将发布日期设置为一个带有自定义字段的日期(之前保存的字段),或者将一篇文章设置为草稿。 唯一的一点是,当调用post new.php时,它也会被激发,那些创建无法删除的“空”帖子的人(以防用户没有填写所有数据并且没有正确保存
post\u save
/post\u updated
操作后调用,因此我们的一所大学向我建议了方法(旧问题:):
它做了它应该做的事情——它将发布日期设置为一个带有自定义字段的日期(之前保存的字段),或者将一篇文章设置为草稿。
唯一的一点是,当调用post new.php
时,它也会被激发,那些创建无法删除的“空”帖子的人(以防用户没有填写所有数据并且没有正确保存帖子)。
我应该如何在该函数中创建一个“check”,使其仅在保存或更新帖子时运行
我在使用函数设置这些post\u数据时也有一个小问题:
add_action( 'save_post', 'cs_twitter_save_meta' );
function cs_twitter_save_meta( $post_id ) {
if ( isset($_POST['post_type']) && 'tweets' == $_POST['post_type'] ) :
if ( isset($_POST['post_title']) ) :
$url = 'https://api.twitter.com/1/statuses/show.json?id='.$_POST['post_title'];
$json = @file_get_contents($url,0,null,null);
if ( $json != false ) {
$json_output = json_decode($json);
$post_date = date('c', strtotime($json_output->created_at));
$post_save_date = date('G:i - d M y', strtotime($json_output->created_at));
update_post_meta( $post_id, '_cs_tweet_content', $json_output->text );
update_post_meta( $post_id, '_cs_tweet_date', $post_save_date );
update_post_meta( $post_id, '_cs_system_tweet_date', $post_date );
update_post_meta( $post_id, '_cs_tweet_user', $json_output->user->screen_name );
update_post_meta( $post_id, '_cs_tweet_ok_status', 'true' );
} else {
update_post_meta( $post_id, '_cs_tweet_content', 'There is an error with tweet Api. Too many connections in one hour. Try again after 60 minutes' );
update_post_meta( $post_id, '_cs_tweet_ok_status', 'false' );
}
endif;
endif;
}
// Autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// AJAX
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
// Post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;
第一次保存时出现问题。似乎add_filter('wp_insert_post_data','set_post_parameters',99,2)代码>首先触发,看不到自定义图元,并将post\U状态设置为草稿。然后add_action('save_post','cs_twitter\u save_meta')代码>激发,但第一个脚本已经太晚了
那么另一个问题-我应该如何解决这种情况,以便首先正确设置所有数据:
1.用户创建新的自定义发布类型。
2.他输入tweets id进行导入,并推送发布。
3.脚本检查是否可以从twitterapi获取数据
4.如果可以,它会提供所有必要的数据,发布帖子,并将发布日期设置为tweet的发布日期。
5.如果没有,它将显示有关错误的信息,并将post\u status
设置为draft。除非明确单击save,否则可以尝试阻止函数运行
在函数开头添加以下代码段:
add_action( 'save_post', 'cs_twitter_save_meta' );
function cs_twitter_save_meta( $post_id ) {
if ( isset($_POST['post_type']) && 'tweets' == $_POST['post_type'] ) :
if ( isset($_POST['post_title']) ) :
$url = 'https://api.twitter.com/1/statuses/show.json?id='.$_POST['post_title'];
$json = @file_get_contents($url,0,null,null);
if ( $json != false ) {
$json_output = json_decode($json);
$post_date = date('c', strtotime($json_output->created_at));
$post_save_date = date('G:i - d M y', strtotime($json_output->created_at));
update_post_meta( $post_id, '_cs_tweet_content', $json_output->text );
update_post_meta( $post_id, '_cs_tweet_date', $post_save_date );
update_post_meta( $post_id, '_cs_system_tweet_date', $post_date );
update_post_meta( $post_id, '_cs_tweet_user', $json_output->user->screen_name );
update_post_meta( $post_id, '_cs_tweet_ok_status', 'true' );
} else {
update_post_meta( $post_id, '_cs_tweet_content', 'There is an error with tweet Api. Too many connections in one hour. Try again after 60 minutes' );
update_post_meta( $post_id, '_cs_tweet_ok_status', 'false' );
}
endif;
endif;
}
// Autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// AJAX
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
// Post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;
这应该可以解决你的问题。不要使用wp\u insert\u post\u data
hook,只使用save\u post
并使用$wpdb更改数据库条目
add_action( 'save_post', 'cs_twitter_save_meta' );
function cs_twitter_save_meta( $post_id ) {
if ( isset($_POST['post_type']) && 'tweets' == $_POST['post_type'] ) :
if ( isset($_POST['post_title']) ) :
$url = 'https://api.twitter.com/1/statuses/show.json?id='.$_POST['post_title'];
$json = @file_get_contents($url,0,null,null);
if ( $json != false ) {
$json_output = json_decode($json);
$post_date = date('Y-m-d H:i:s', strtotime($json_output->created_at));
$post_date_gmt = get_gmt_from_date( $post_date );
$post_save_date = date('G:i - d M y', strtotime($json_output->created_at));
update_post_meta( $post_id, '_cs_tweet_content', $json_output->text );
update_post_meta( $post_id, '_cs_tweet_date', $post_save_date );
update_post_meta( $post_id, '_cs_system_tweet_date', $post_date );
update_post_meta( $post_id, '_cs_tweet_user', $json_output->user->screen_name );
update_post_meta( $post_id, '_cs_tweet_ok_status', 'true' );
global $wpdb;
$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_date = '$post_date', post_date_gmt = '$post_date_gmt' WHERE id = $post_id", $post_id ));
} else {
update_post_meta( $post_id, '_cs_tweet_content', 'There is an error with tweet Api. Too many connections in one hour. Try again after 60 minutes' );
update_post_meta( $post_id, '_cs_tweet_ok_status', 'false' );
global $wpdb;
$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'draft' WHERE id = $post_id", $post_id ));
}
endif;
endif;
}