Custom Fields
Re-Envisioned
Support MB Relationships Unable to display related posts: Fatal error

  • Creator
    Topic
  • #17028
    Resolved sunil@sunil.co.nz
    Participant

    I have two post types: ‘Conversation’ and ‘Person’.

    When creating a Conversation, a user can select people to be related to the Conversation.
    I created the relationship like this:

    add_action( 'mb_relationships_init', function() {
        MB_Relationships_API::register( array(
            'id'   => 'connect_conversations_to_people',
            'from' => 'conversation',
            'to'   => 'person',
        ) );
    } );

    This works: the user can indeed select People when creating a Conversation post.

    In the Page Template for displaying the Conversation, I want to display the People who are related to the Conversation. I’ve basically copied code from the documentation for MB Relationships:

    $connected = new WP_Query( array(
                'relationship' => array(
                    'id'   => 'posts_to_pages',
                    'from' => get_the_ID(), // You can pass object ID or full object
                ),
                'nopaging'     => true,
            ) );
            while ( $connected->have_posts() ) : $connected->the_post();
            ?>
                <a>"><?php the_title(); ?></a>
            <?php
            endwhile;
            wp_reset_postdata();

    This yields the fatal error:

    Fatal error: Uncaught Error: Call to a member function get_db_field() on null in /var/www/mysite.localdev/wp-content/plugins/meta-box-aio/vendor/meta-box/mb-relationships/inc/query/normalizer.php on line 75
    

    What could be happening here?

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #17041
    sunil@sunil.co.nz
    Participant

    I figured it out.

    In the function to display fields from the related CPT’s, I should have used the correct id: the id of the relationship that I initially created.

    So the corrected function reads like this:

    $connected = new WP_Query( array(
    ‘relationship’ => array(
    ‘id’ => ‘connect_conversations_to_people’,
    ‘from’ => get_the_ID(), // You can pass object ID or full object
    ),
    ‘nopaging’ => true,
    ) );
    while ( $connected->have_posts() ) : $connected->the_post();
    ?>
    “><?php the_title(); ?>
    <?php
    endwhile;
    wp_reset_postdata();
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.