每当发布wordpress "Jobs“自定义帖子时,我都会尝试运行以下函数。当代码放在我的主题模板中时,它可以工作(第2-7行),但它只在文章被查看时运行。我希望代码在发布帖子时运行,因此我尝试在functions.php内的函数中添加代码,但在发布每个自定义帖子时没有任何反应。有什么建议吗?
function indeedgeo(){
$indeedgeo = get_post_meta($post>ID, indeedgeo, true);
$indeedgeos=explode(' ',$indeedgeo);
$_jr_geo_latitude = $indeedgeos[0];
$_jr_geo_longitude = $indeedgeos[1];
update_post_meta($post->ID, _jr_geo_latitude, $_jr_geo_latitude);
update_post_meta($post->ID, _jr_geo_longitude, $_jr_geo_longitude);
}
add_action('publish_Jobs', 'indeedgeo');
发布于 2012-05-04 13:45:27
你应该钩住三个动作中的一个;
do_action('edit_post', $post_id, $post);
do_action('save_post', $post_id, $post);
do_action('wp_insert_post', $post_id, $post);
在保存帖子或更新帖子状态时运行。下面这样的代码应该可以解决这个问题。
function se_10441543_save_post($post_id, $post){
//determine post type
if(get_post_type( $post_id ) == 'your_post_type'){
//run your code
$indeedgeo = get_post_meta($post_id, indeedgeo, true);
$indeedgeos=explode(' ',$indeedgeo);
$_jr_geo_latitude = $indeedgeos[0];
$_jr_geo_longitude = $indeedgeos[1];
update_post_meta($post_id, _jr_geo_latitude, $_jr_geo_latitude);
update_post_meta($post_id, _jr_geo_longitude, $_jr_geo_longitude);
}
}
add_action('save_post', 'se_10441543_save_post', 10, 2);
http://codex.wordpress.org/Plugin_API/Action_Reference
发布于 2012-05-04 13:03:46
不是很确定你的"publish_jobs“钩子是如何工作的,但是如果你把这个函数放在你的functions.php中,你需要给它一个上下文。将$post>ID
替换为帖子编号(整数)。如果这适用于许多帖子,那么您可能希望使用另一种查询帖子数据的方法:http://codex.wordpress.org/Class_Reference/WP_Query。如果这有帮助,请告诉我。
https://stackoverflow.com/questions/10441543
复制相似问题