Relationships Post Query Not Fetching
Support › MB Relationships › Relationships Post Query Not Fetching
- This topic has 3 replies, 2 voices, and was last updated 1 year, 1 month ago by
Peter.
-
AuthorPosts
-
March 31, 2024 at 1:20 PM #45076
davidrknowles
ParticipantHello Everyone, It has been a while.
I am having a go at setting up a basic client area, I have two main CPT: project and client.
I have two relationships: user_client and client_projects.
These work fine in other filtering envioroments(I have beaver builder fl_builder_loop_query_args and this filters it perfectly for the post module), but for the Singular posts/projects it is not playing nice.I does the default behaviour when no post can be found (error404), but when it is the correct item it just returns standard post archive and the message "Sorry, we couldn't find any posts. Please try a different search." that I think are standard beaver builder templete behavour when empy post is returned.
Cant figure it out, it is definity the relationship args that are effecting it, but can pin point what.
function main_project_query( $query ) { if ( ! is_admin() && $query->is_main_query() ) { if (is_user_logged_in() && current_user_can('administrator') ) { return; } else { $user_id = get_current_user_id(); $client = MB_Relationships_API::get_connected( [ 'id' => 'user_client', 'from' => $user_id ] ); if (count($client) > 0) { $client = $client[0]; $client_id = $client->ID; } else { $client_id = null; } $query->set( 'relationship', [ 'id' => 'client_projects', 'from' => $client_id ]); } } } add_action( 'parse_query', 'main_project_query' );
Thanks
DavidMarch 31, 2024 at 9:01 PM #45079davidrknowles
ParticipantJust running through the debug, I get the following database error:
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) WHERE 1=1 AND wp_posts.post_name = 'scott-street' AND wp_posts.post_t...' at line 2]
SELECT wp_posts.* , mbr.to FROM wp_posts INNER JOIN wp_mb_relationships AS mbr ON (mbr.from = wp_posts.ID AND mbr.type = 'client_projects' AND mbr.to IN ()) WHERE 1=1 AND wp_posts.post_name = 'scott-street' AND wp_posts.post_type = 'project' GROUP BY mbr.to, wp_posts.ID ORDER BY mbr.order_to
March 31, 2024 at 9:38 PM #45080davidrknowles
ParticipantThe above error goes away if I specify a "to" in the query, but this doesnt work well if I am using a slug and not an ID, as I dont think ID is availible in the loop yet.
This also doesnt solve the problem of it returning nothing and the theme replacing it with the archive layout.
$query->set( 'relationship', [ 'id' => 'client_projects', 'from' => 178, 'to' => 107 ]);
April 1, 2024 at 8:59 PM #45092Peter
ModeratorHello,
The extension MB Relationship only saves the ID of the object to the database, so you have to use the ID in the
relationship
argument.
If it still doesn't work, please share the code that creates the relationship between the client and project on your site. -
AuthorPosts
- You must be logged in to reply to this topic.