scribu / wp-posts-to-posts

Efficient many-to-many connections between posts, pages, custom post types, users.
http://wordpress.org/plugins/posts-to-posts/
969 stars 260 forks source link

P2P->connect() : Fatal error: Call to a member function connect() on boolean #547

Open gillian37 opened 7 years ago

gillian37 commented 7 years ago

Hi,

I'm trying to connect my Sub-category post type to a category post type in the following function, but I have this error message : Fatal error: Call to a member function connect() on boolean.

When I echo the $p2p value, it displays "false", but why ?

Anyone could help me ? Thanks!

Import function :

`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();
        }
    }

}`

Connection types : (in functions.php)

`function my_connection_types() {

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

}`

romadev commented 5 years ago

Hi Gillian!

Years later: Have you found any solutions to this problem?

Thanks!