Relationship is created even whit invalid post ID

Support MB Relationships Relationship is created even whit invalid post ID

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #35805

    Hello,

    We have a code where we are creating the relationships between two objects, and we want the posts to be deleted if the relationship are not created due to the second object non existence.

    private function create_relationship(String $yearId, String $modelId): bool {
        return \MB_Relationships_API::add($yearId, $modelId, 'yearModel', 1, 1);
    }
    

    However, this function returns true, even when we force an inexistent ID. Going to the WordPress interface we checked that the object does not have any object related on the select box of the relationship. We also tried to validate with the following code:

    \MB_Relationships_API::has($modelId, $brandId, 'modelBrand');
    

    Why does it happen? Is there a way we can be sure the relationship will not be created?

    #35813
    Long NguyenLong Nguyen
    Moderator

    Hi,

    I think you can check if the variable $modelId is not empty and then create the relationship.

    private function create_relationship(String $yearId, String $modelId): bool {
        if( !empty( $modelId ) ) {
            return \MB_Relationships_API::add($yearId, $modelId, 'yearModel', 1, 1);
        }
        return false;
    }

    Or follow this topic to check if a post existed by ID https://stackoverflow.com/questions/9638375/check-existence-of-a-post-by-id-in-wordpress

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