Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Term exist not response #10

Open
tuyenlaptrinh opened this issue Jul 19, 2024 · 1 comment
Open

Term exist not response #10

tuyenlaptrinh opened this issue Jul 19, 2024 · 1 comment

Comments

@tuyenlaptrinh
Copy link

Hello. I glad when see your package. Everything working very good.
I checked when I create duplicate category name. It will return error because term is exist. It should return term id. I think.

@tuyenlaptrinh
Copy link
Author

tuyenlaptrinh commented Jul 19, 2024

I added this

class TermExistsException extends WordPressException
{
    public $data;
    public function __construct(WordPressError $error)
    {
        parent::__construct($error, 400);
        $this->data = $error->data;
    }

    public function getData()
    {
        return $this->data;
    }
}

and my controller

public function createCategory($data)
        {
            try {
                return $this->wp->category()->create($data);
            }
            catch (WordPressException $exception){
                if (method_exists($exception, 'getData') && ($data = $exception->getData()) && !empty($data->term_id)) {
                    return $this->wp->category()->retrieve($data->term_id);
                }
                return false;
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant