• Hello!
    I have created a relationship of “user” to “something”(custom post type) with ID users_to_something.
    Then I try to query the relationship between them.

    <?php

    
    global $post;
    $args = array(
        'post_type'      => 'something',
        'posts_per_page' => -1,
    );
    MB_Relationships_API::each_connected(
        array(
            'id'       => 'users_to_something',
            'from'     => $test_query->posts, // tried both to/from
            'property' => 'clients',
        )
    );
    
    $test_query = new WP_Query( $args );
    
    while ( $test_query->have_posts() ) :
        setup_postdata( $post );
        $test_query->the_post();
    
        var_dump($post); // the relationship parameter "clients" has nothing inside
    
        // Display connected pages (not working because there is no relationships found by each_connected)
        foreach ( $post->clients as $p ) :
            // More core here...
        endforeach;
    
        // Get individually(Working properly.).
        $users = get_users(
            array(
                'relationship' => array(
                    'id' => 'users_to_something',
                    'to' => get_the_ID(), 
                ),
            )
        );
        foreach ( $users as $user ) {
            echo $user->display_name;
        }
    
    endwhile;
    
    

    The “each_connected” function seems not working as expected but query separately using “get_user” seems to working.
    In this code, is there anything missed in the “each_connected”?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter 西門 正 Code Guy

    (@simongcc)

    File an issue in github instead after tracing the code.
    https://github.com/wpmetabox/mb-relationships/issues/56

    Plugin Author Anh Tran

    (@rilwis)

    Hi @simongcc ,

    I’ve seen your issue on GH. I’ll check and fix it.

    Thank you very much for your feedback!

    Plugin Author Anh Tran

    (@rilwis)

    Hi @simongcc,

    Just found the problem. In your code, the connection is FROM users TO posts, so in the code, when you want to get users that connect TO posts, the parameter should be “TO” instead of “FROM”:

    MB_Relationships_API::each_connected(
        array(
            'id'       => 'users_to_something',
            'to'       => $test_query->posts, // THIS
            'property' => 'clients',
        )
    );

    Also, there’s a bug in checking user prop. We have fixed it on Github and will release a new version soon.

    • This reply was modified 2 years, 11 months ago by Anh Tran.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘MB_Relationships_API::each_connected does not fetch anything’ is closed to new replies.