Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
发布自定义帖子类型时在Wordpress中运行函数_Wordpress - Fatal编程技术网

发布自定义帖子类型时在Wordpress中运行函数

发布自定义帖子类型时在Wordpress中运行函数,wordpress,Wordpress,每当发布wordpress“Jobs”自定义帖子时,我都会尝试运行以下函数。当代码放在我的主题模板中时,它可以工作(第2-7行),但它只在查看帖子时运行。我希望在发布文章时运行代码,因此我尝试将代码添加到functions.php中的函数中,但在发布每个自定义文章时都没有发生任何事情。有什么建议吗 function indeedgeo(){ $indeedgeo = get_post_meta($post>ID, indeedgeo, true); $indeedgeos=

每当发布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');

不太清楚“publish_jobs”钩子是如何工作的,但如果要将此函数放在functions.php中,则需要提供上下文。用邮政编码(整数)替换
$post>ID
。如果这适用于许多帖子,您可能需要使用另一种查询帖子数据的方法:。让我知道这是否有帮助。

不太清楚“发布作业”挂钩的工作原理,但如果要将此函数放置在functions.php中,则需要提供上下文。用邮政编码(整数)替换
$post>ID
。如果这适用于许多帖子,您可能需要使用另一种查询帖子数据的方法:。让我知道这是否有帮助。

你应该在三个动作中选择一个

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);

你应该在三个动作中选择一个

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);

这是有道理的,但在发布/更新自定义帖子时没有任何作用。代码已经添加到functions.php中,我唯一更改的是输入自定义的post类型名称。//运行您的代码部分在插入到我的主题模板时工作正常。如果添加wp_die(print_r($post));在对你的帖子类型进行if测试之前,当你试图发布你的帖子时,它会说什么?这至少表明行动正在进行。谢谢你的回复。添加了这一行后,我在尝试从rss提要批量导入帖子时得到了以下信息(这是通常的方法):
stdClass对象([ID]=>3952[post\u author]=>1[post\u date]=>2012-05-0417:35:43[post\u date\u gmt]=>2012-05-0417:35:43[post\u content]=>
如果我尝试手动添加自定义帖子,我会得到以下信息:`stdClass对象([ID]=>3953[post\u author]=>1[post\u date]=>2012-05-04 21:33:54[post\u date\u gmt]=>0000-00-00:00:00[post\u content]=>[post\u title]=>Auto Draft[post\u status]=>Auto Draft[comment\u status]=>open[ping\u status]=>open[post\u密码]=>[post\u name]=>[ping]=>[ping]=>[post\u modified]=>2012-05:54[post\u modified\u gmt]=>0000-00-00 00:00:00[post\u内容过滤]=>[post\u父项]=>0[guid]=>0[post\u类型]=>job\u列表[[[post\u类型]=>job\u列表[[code>[post\u类型]=>post\u mime\u类型]=>0[comment\u计数]=>0[祖先]=>Array()[filter]=>raw)1我猜这显示的是所有正在运行的函数?我没有在那里看到我的函数,因此它似乎没有运行?这很有意义,但在自定义帖子发布/更新时没有任何作用。代码已添加到functions.php,我唯一更改的是输入自定义帖子类型名称。//运行代码部分插入到我的主题模板中时效果很好。如果您添加wp_模具(print_r($post));在对您的帖子类型进行if测试之前,当您尝试发布帖子时,它会说什么?这至少表明该操作正在被调用。感谢您的响应。添加这行代码后,当尝试从rss提要批量导入帖子时(与通常的方法一样):
stdClass对象([ID]=>3952[post\u author]=>1[post\u date]=>2012-05-04 17:35:43[post\u date\u gmt]=>2012-05-04 17:35:43[post\u content]=>
如果我尝试手动添加自定义帖子,我会得到以下结果:`stdClass对象([ID]=>3953[post\u author]=>1[post\u date]=>2012-05-04 21:33:54[post\u date\u gmt]=>0000-00-00:00[post\u content]=>[帖子摘录]=>[帖子状态]=>自动起草[评论状态]=>打开[点击状态]=>打开[帖子密码]=>[帖子名称]=>[点击]=>[点击]=>[修改帖子]=>2012-05-04 21:33:54[修改帖子gmt]=>0000-00-00:00[过滤帖子内容]=>[帖子家长]=>0[guid]=>[菜单顺序]=>0[帖子类型]>作业列表[[[由于注释长度而中断]]]以下是剩余内容:
[post\u type]=>作业列表[post\u mime\u type]=>[comment\u count]=>0[祖先]=>Array()[filter]=>raw)1
我猜这会显示所有正在运行的函数?我在那里看不到我的函数,因此它似乎没有运行?发布作业挂钩信息取自发布作业挂钩信息取自