将插件应用于自定义帖子类型?

时间:2013-07-16 作者:mantis

我正在尝试重新构造一个函数(在插件中),以便它应用于自定义帖子类型中的帖子,而不是普通帖子。

 function saving_posts($post_id)
{
    global $wpdb, $user_ID;
    $this->setup_links();
    if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
        return;
    if(\'post\' == $_POST[\'post_type\'])
    {
        if(!current_user_can(\'edit_post\', $post_id))
            return;
    }
    else
        return;
    $mydata = ($_POST[\'mf_post_to_forum\'] == \'true\')?true:false;
    if($mydata)
    {
        $date = $this->wpf_current_time_fixed(\'mysql\', 0);
        $fid = (int)$_POST[\'mf_post_to_forum_forum\'];
        $_POST[\'mf_post_to_forum\'] = \'false\'; //Eternal loop if this isn\'t set to false
        $post = get_post($post_id);
        $sql_thread = "INSERT INTO {$this->t_threads} (last_post, subject, parent_id, `date`, status, starter) VALUES(\'{$date}\', \'".$this->strip_single_quote($post->post_title)."\', \'{$fid}\', \'{$date}\', \'open\', \'{$user_ID}\')";
        $wpdb->query($sql_thread);
        $tid = $wpdb->insert_id;
        $sql_post = "INSERT INTO {$this->t_posts} (text, parent_id, `date`, author_id, subject) VALUES(\'".$this->input_filter($wpdb->escape($post->post_content))."\', \'{$tid}\', \'{$date}\', \'{$user_ID}\', \'".$this->strip_single_quote($post->post_title)."\')";
        $wpdb->query($sql_post);
        $new = $post->post_content."\\n".\'<p><a href="\'.$this->get_threadlink($tid).\'">\'.__("Join the Forum discussion on this post", "mingleforum").\'</a></p>\';
        $post->post_content = $new;
        wp_update_post($post);
    }
} 
我想我可以改变一下if(\'post\' == $_POST[\'post_type\'])if(\'my_custom_post\' == $_POST[\'post_type\']) 其余的也一样。这个$mydata价值来自这里:

function send_wp_posts_to_forum()
{
    add_meta_box(\'mf_posts_to_forum\', __(\'Mingle Forum Post Options\', \'mingleforum\'), array(&$this, \'show_meta_box_options\'), \'my_custom_post\');
}
function show_meta_box_options()
{
    $forums = $this->get_forums();
    echo \'<input type="checkbox" name="mf_post_to_forum" value="true" />&nbsp;\'.__(\'Add this post to\', \'mingleforum\');
    echo \'&nbsp;<select name="mf_post_to_forum_forum">\';
    foreach($forums as $f)
        echo \'<option value="\'.$f->id.\'">\'.$f->name.\'</option>\';
    echo \'</select><br/><small>\'.__(\'Do not check this if this post has already been linked to the forum!\', \'mingleforum\').\'</small>\';
}
这很好,因为元框位于正确的位置。

1 个回复
SO网友:mantis

问题在于$this->setup_links(); 其中使用add_action("publish_post", array(&$this, "saving_posts")); 而不是add_action("save_post", array(&$this, "saving_posts"));

显然地publish_post 不适用于自定义帖子类型。

结束

相关推荐

使用哪种查询方法?(EDIT-wpdb语法问题)

我正在尝试创建一个查询,以便从名为match\\u reports的自定义帖子类型中获取所有元数据,该帖子类型与另一个名为team\\u的自定义帖子类型中的meta\\u键和meta\\u值相关联。公共分母是team\\u页面的id,它是match\\u报告中的meta\\u值。因此,无论是主队还是客队,每支球队都有与其相关的比赛报告。最终,结果将在一个表格中循环,以便每个团队都列在自己的统计数据旁边。我尝试使用WP\\u Query来执行此操作,但运气不佳:Looping through tabular