2017-03-15 72 views
1

我正在使用Post 2 Post插件创建帖子类型之间的关系,并且我试图连接Sub -type category post type to a category post type in the following function,but I have this error message:致命错误:调用布尔型的成员函数connect()。P2P-> connect():致命错误:调用布尔型的成员函数connect()

当我回显$ p2p值时,它显示“false”,但为什么?

任何人都可以帮助我吗?谢谢!

导入功能:

function import_subCategories(){ 

    global $co; 

    $query = "SELECT * FROM `tl_events_sous_categories`"; 
    $result = $co->query($query); 

    while($data = mysqli_fetch_array($result)){ 

     $my_post = array(

      'post_title' => iconv('ISO-8859-1','UTF-8', $data['headline']), 
      'post_content' => '', 
      'post_status' => 'publish', 
      'post_author' => 1, 
      'post_type' => 'sub-categorie' 
     ); 

    $the_post_id = wp_insert_post($my_post); 

    // Create the relationship between CATEGORY and SUB CATEGORY 
    $p2p = p2p_type('sous_categories_categorie'); 
    $p2p->connect($the_post_id, $data['pid'], array('date' => current_time('mysql'))); 

    if(is_wp_error($p2p)){ 
     echo $p2p->get_error_message(); 
    } 
} 

}`

连接类型:(中的functions.php)

function my_connection_types() { 

    p2p_register_connection_type(array(
     'name' => 'sous_categories_categorie', 
     'from' => 'sub-categorie', 
     'to' => 'categorie' 
    )); 
    } 

编辑:看来我的连接类型被注册,因为它出现在WP工具菜单: WP Tools menu

回答

0

This line

$p2p = p2p_type('sous_categories_categorie'); 

返回false,因为显然“sous_categories_categorie”不是有效的连接类型。

The function p2p_type is defined as

/** 
* Get a connection type. 
* 
* @param string $id Connection type id 
* 
* @return bool|object False if connection type not found, P2P_Connection_Type instance on success. 
*/ 
function p2p_type($id) { 
    return P2P_Connection_Type::get_instance($id); 
} 

现在你只需要找出原因。考虑在拨打p2p_type之前,请先拨

var_dump(P2P_Connection_Type::get_all_instances()) 

查看实际注册的类型。

确保在注册类型中没有错别字,并且您在尝试使用p2p_type找到它之前注册了连接类型。请注意,连接类型不应该在'init'钩子之前注册。

+0

谢谢你的回答!传递给p2p_type()的$ id参数是p2p_register_connection_type()中使用的'name'值吗?因为这是我做的,但不起作用 – Gillian

+0

@Gillian从我可以从源代码中得知,是的。查看我的更新。 – Gordon

+0

感谢您的指示。看起来P2P_Connection_Type不存在(不再?),所以我改用P2P_Connection_Type_Factory。我有一个空的数组...所以,我的连接类型没有注册,但我不知道为什么..我不知道我的代码有什么问题... – Gillian

相关问题