- This topic has 6 replies, 2 voices, and was last updated 2 years, 6 months ago by .
-
Topic
-
I haven’t been able to get any posts to display with the ‘relationship’ parameter added to WP_Query, even on a fresh install of WordPress running the default Twenty Seventeen theme, and only having installed the Meta Box (4.14.11), MB Custom Post Type (1.8.0), and MB Relationships (1.3.0) plugins.
Let me know if I’m doing something wrong. Here are the steps I took:
After installing the 3 plugins, I created custom post types of ‘event’ and ‘speaker’ with default options selected. Then I created a few events and speakers with basic titles. I inserted this code into functions.php to create a relationship:
// MB Relationship: Event to Speaker add_action('mb_relationships_init', function () { MB_Relationships_API::register(array( 'id' => 'event_to_speaker', 'from' => array ( 'object_type' => 'post', 'post_type' => 'event', 'meta_box' => array ( 'title' => 'Event speakers', 'field_title' => 'Select speakers for this event', ), ), 'to' => array ( 'object_type' => 'post', 'post_type' => 'speaker', 'meta_box' => array ( 'title' => 'Associated events', 'empty_message' => 'No events', ), ), )); });
Then I used the admin panel meta boxes to connect each event to at least one speaker. This is the code I inserted into single.php to test for titles of speakers for each event (almost a direct copy from the documentation):
// Get speakers for this event $connected = new WP_Query( array( 'relationship' => array( 'id' => 'event_to_speaker', 'to' => get_the_ID(), ), 'nopaging' => true, )); echo '<p>the title(s) should be right below here ↓</p>'; while ( $connected->have_posts() ) : $connected->the_post(); the_title(); endwhile; wp_reset_postdata(); echo '<p>the title(s) should be right above here ↑</p>';
Yet, no speaker titles display between those lines. Am I missing something?
- You must be logged in to reply to this topic.