Relationships Post Query Not Fetching

Support MB Relationships Relationships Post Query Not Fetching

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #45076
    davidrknowlesdavidrknowles
    Participant

    Hello 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
    David

    #45079
    davidrknowlesdavidrknowles
    Participant

    Just 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

    #45080
    davidrknowlesdavidrknowles
    Participant

    The 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
    ]);
    #45092
    PeterPeter
    Moderator

    Hello,

    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.

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