Support Forum
Support › MB Relationships › No Relationship posts displayingResolved
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?
Hi Doug,
I've tested your code and found the bug: In the query for single.php, it should be 'from' => get_the_ID()
. Your connection is from event to speakers, so if you're on event page, the connection direction should be "from" this event.
Thanks, Anh. I swear I tried both 'from' and 'to' when I was trying to get anything to work. But I just switched it, and sure enough, 'from' pulls in the correct speaker posts.
Thank you again! Creating these relationships is so awesomely simple and powerful with MB.
So I think i figured out what the issue was, in addition to leaving in 'to' instead of 'from' as I was testing everything I could think of. I actually have several custom post types created in addition to 'event' and 'speaker', including 'location' and 'sponsor'. I was originally trying to query posts via the relationship of event_to_location. That was the one that wouldn’t return anything no matter what I did
I just switched the query back from speaker to location, and no posts returned again. But I went into the custom post types and compared the settings for each. My 'location' CPT had 'exclude from search' as true, but speaker was false. Sure enough, toggling the 'exclude from search' setting on breaks the display of MB Relationship posts. Is that expected behavior? I would expect turning off 'publicly queryable' would block the display. But not exclude from search?
Hi Doug,
It's unexpected behaviour, but it has a reason. To make the query can get posts from any side of the connection, the plugin sets the post_type
to any
in the query. And WP_Query
checks for exclude_from_search
in this case. See this:
https://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters
'any' - retrieves any type except revisions and types with 'exclude_from_search' set to true.
I don't know why WordPress sets that :(. It seems kind of weird to me.
Ah, ok. Makes sense, but that is strange.
Given this, I thought I might be able to explicitly specify the post_type alongside 'relationship' in the query and still keep those CPTs set to exclude_from_search. But that didn't work. Probably due to the way the plugin modifies the query. I’ll have to hide those custom post types from search another way. No worries.
Let me update the extension to remove the post_type
rule. People still can set the post types anyway.
Update 1: Done! Version 1.3.1 is available.
Update 2: I was too rush. The 'post_type' seems to be related to many things. I have to revert it to the previous version.