2017-03-15 75 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

相關問題