Trying to display all the possible connection to a post

Support MB Relationships Trying to display all the possible connection to a post

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #13283
    Vee JayVee Jay
    Participant

    Here's my code

    $my_query = new WP_Query( array(
        'post_type' => array( 'post', 'page', 'conference', 'publication', 'press'),
        'post_status' => array ('future', 'publish'),
        'paged' => $paged,
        'posts_per_page' => 24
    ) );
    
    MB_Relationships_API::each_connected( array(
        'id'       => 'conf2pub',
        'from'     => $wp_query->posts,
        'property' => 'connected_confs',
    ) );
    
    MB_Relationships_API::each_connected( array(
        'id'       => 'post2pub',
        'from'     => $wp_query->posts,
        'property' => 'connected_posts',
    ) );
    
    MB_Relationships_API::each_connected( array(
        'id'       => 'pub_and_press',
        'from'     => $wp_query->posts,
        'property' => 'connected_press_items',
    ) );
    
    while ( $my_query->have_posts() ) : $my_query->the_post();
    
        // Display connected pages
        foreach ( $post->connected as $post ) : setup_postdata( $post );
            the_title();
        endforeach;
        wp_reset_postdata(); // Set $post back to original post.
    
    endwhile;

    What am I doing wrong? I followed the documentation but it's not working...

    #13286
    Vee JayVee Jay
    Participant

    I even tried the following

    $related = new WP_Query( array(
        'relationship' => array(
            'relation' => 'OR',
            array(
                'id'   => 'conf2pub',
                'to' => get_the_ID(),
            ),
            array(
                'id'   => 'post2pub',
                'to' => get_the_ID(),
            ),
            array(
                'id'   => 'pub_and_press',
                'from' => get_the_ID(),
            ),
        ),
        'nopaging'     => true,
    ));
    while ( $related->have_posts() ) : $related->the_post();
        the_title();
    endwhile;
    wp_reset_postdata();
    #13295
    Anh TranAnh Tran
    Keymaster

    Hi Vee Jay,

    In MB_Relationships_API::each_connected, you set property different for each connection. So to get them all, use this code:

    while ( $my_query->have_posts() ) : $my_query->the_post();
    
        // Display connected pages
        foreach ( $post->connected_confs as $post ) : setup_postdata( $post );
            the_title();
        endforeach;
        wp_reset_postdata();
    
        foreach ( $post->connected_posts as $post ) : setup_postdata( $post );
            the_title();
        endforeach;
        wp_reset_postdata();
    
        foreach ( $post->connected_press_items as $post ) : setup_postdata( $post );
            the_title();
        endforeach;
    
        wp_reset_postdata();
    endwhile;
    #13316
    Vee JayVee Jay
    Participant

    I tried this and it still doesn't work :

    $my_query = new WP_Query( array(
        'post_type' => array( 'post', 'page', 'conference', 'publication', 'press'),
        'post_status' => array ('future', 'publish'),
        'paged' => $paged,
        'posts_per_page' => 24
    ) );
    
    MB_Relationships_API::each_connected( array(
        'id'       => 'conf2pub',
        'from'     => $my_query->posts,
        'property' => 'connected_confs',
    ) );
    
    MB_Relationships_API::each_connected( array(
        'id'       => 'post2pub',
        'from'     => $my_query->posts,
        'property' => 'connected_posts',
    ) );
    
    MB_Relationships_API::each_connected( array(
        'id'       => 'pub_and_press',
        'from'     => $my_query->posts,
        'property' => 'connected_press_items',
    ) );
    
    while ( $my_query->have_posts() ) : $my_query->the_post();
    
        foreach ( $post->connected_confs as $post ) : setup_postdata( $post );
            the_title();
        endforeach;
        wp_reset_postdata();
    
        foreach ( $post->connected_posts as $post ) : setup_postdata( $post );
            the_title();
        endforeach;
        wp_reset_postdata();
    
        foreach ( $post->connected_press_items as $post ) : setup_postdata( $post );
            the_title();
        endforeach;
    
        wp_reset_postdata();
    endwhile;

    I get a " Invalid argument supplied for foreach() in " error.

    #13323
    Anh TranAnh Tran
    Keymaster

    Hi Vee Jay, I've tried the code and it works for me. Here is my video record: http://recordit.co/1e1j1PkNs5

    And here is the code I used to test: https://ghostbin.com/paste/kyufr

    #13389
    Vee JayVee Jay
    Participant

    I think I understand what the problem is.
    You are using this code to display in an archive page where all your posts are listed. But what about when you are inside a single post?
    Would the code change? Here's what I have in both function.php and the single post: https://ghostbin.com/paste/ksghe

    #13390
    Vee JayVee Jay
    Participant

    I can get it to work like this:

    $connected_conf2pub = new WP_Query( array(
                'relationship' => array(
                    'id'   => 'conf2pub',
                    'to' => get_the_ID(), // You can pass object ID or full object
                ),
                'nopaging'     => true,
            ) );
    
            while ( $connected_conf2pub->have_posts() ) : $connected_conf2pub->the_post();
                echo 'Connected Conf: <a href="' . get_permalink($post->ID) . '"><p>' . get_the_title($post->ID) . '</p></a>';
            endwhile;
            wp_reset_postdata();
    
            $connected_post2pub = new WP_Query( array(
                'relationship' => array(
                    'id'   => 'post2pub',
                    'to' => get_the_ID(), // You can pass object ID or full object
                ),
                'nopaging'     => true,
            ) );
            while ( $connected_post2pub->have_posts() ) : $connected_post2pub->the_post();
                echo 'Connected Posts: <a href="' . get_permalink($post->ID) . '"><p>' . get_the_title($post->ID) . '</p></a>';
            endwhile;
            wp_reset_postdata();
    
            $connected_pub2press = new WP_Query( array(
                'relationship' => array(
                    'id'   => 'pub_and_press',
                    'from' => get_the_ID(), // You can pass object ID or full object
                ),
                'nopaging'     => true,
            ) );
            while ( $connected_pub2press->have_posts() ) : $connected_pub2press->the_post();
               echo 'Connected Press: <a href="' . get_permalink($post->ID) . '"><p>' . get_the_title($post->ID) . '</p></a>';
            endwhile;
            wp_reset_postdata();

    But would like to be able to display all the connections as one for design purposes. I don't really need to identify what's what, I just want to display all connections in date order.

    #13394
    Anh TranAnh Tran
    Keymaster

    Hi Vee Jay,

    In the first post, you used the each_connected function to demo your purpose. So, I thought you were working on an archive template. It's great that you have resolved the problem for single post.

    Regarding to list all posts in all relationships at once, we're working on that. It'll be available in the next version of the plugin.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.