Last updated: October 25, 2026
Quick Answer: How to validate WordPress post data before automation sends it to the API usually comes down to 5 checks: required fields, field types, slug rules, content sanitization, and schema validation. WordPress post data validation is the check that stops bad JSON payloads before your automation calls the REST API. If a title is too long, a slug is invalid, a required field is missing, or the content contains unsafe markup, the automation should fail in your app—not after WordPress has already been asked to accept it.
What WordPress post data validation needs to catch
Validation needs to catch format errors, missing required fields, and content that WordPress will reject or quietly change. That includes the obvious fields like title, content, status, and slug, plus less obvious rules such as slug length, allowed post statuses, and whether your content sanitization will strip tags that matter to the final post.
The point is to keep your automation from sending a request that cannot succeed as intended. A JSON payload can be syntactically valid and still be wrong for WordPress. A title can be present and still be unusable if it is blank after trimming whitespace. A slug can look human-readable and still fail if it contains spaces, uppercase characters, or characters your workflow cannot normalize cleanly.
This matters most in article automation workflows, where content is assembled by several steps before it ever reaches the WordPress REST API. If one upstream step produces an empty excerpt or an overlong slug, the later step should not have to guess what happened. It should receive a payload that already matches your validation rules.
The honest trade-off is speed versus certainty. Extra validation takes a little more code, usually in your own application or middleware, but it saves you from chasing failures across a queue, a webhook, and a publish request. This approach is not for teams that want to “send first and repair later.” It is for teams that want predictable publishing behavior.
For the WordPress REST API itself, the main documentation is the WordPress REST API handbook. For JSON structure and escaping rules, the MDN JSON guide is a useful reference when you are shaping payloads.
Which fields should be validated before sending a WordPress post?
The fields that should be validated first are the ones WordPress cannot infer safely: title, content, slug, status, and any taxonomy or custom field your automation depends on. If your workflow creates featured images, author IDs, categories, or tags, those need validation too, because an invalid ID can break the request even when the text looks fine.
Start with required fields. For most automation flows, a post without a usable title is a failed input, not a draft. If your workflow allows drafts, then status=draft is a valid state, but the rest of the post still needs to be structurally sound. A draft with an empty title and broken HTML is still bad data.
Then validate type and shape. A title should be a string, not a number or array. Content should be a string or a well-defined block structure, not a nested object invented by a script. Status should be one of the values your site actually uses, usually something like draft, pending, private, or publish depending on permissions and workflow.
A useful rule is to validate the contract, not the hope. If your automation assumes a 1:1 mapping between source article fields and WordPress post data, declare that mapping in code and reject anything that does not fit it. That is stricter than “try and see,” but it avoids accidental publishing of partial posts.
One thing many generic guides miss: taxonomy IDs and custom fields often fail later than the text fields do. A category ID that does not exist may not be obvious from the payload alone. Validate those IDs against known lists before the API call, or your automation will create a request that looks complete and still fails in the final step.
How do I validate a WordPress post payload step by step?
You validate a WordPress post payload by checking structure, field rules, sanitization, and schema compliance before the request leaves your system. A practical pre-flight pass usually takes less than a second, and it is easier to debug than a failed API response after several automation steps have already run.
-
Parse the raw object into a strict JSON structure. Confirm that the payload is valid JSON, not a half-built string or mixed object. The result should be a predictable object with named keys, not a free-form blob that changes from one run to the next.
-
Check required fields and allowed types. Verify that
titleandcontentare strings,statusis an accepted value, and any required IDs are integers. If a field is optional, leave it out instead of sending an empty placeholder unless your schema requires the field explicitly. -
Trim and normalize text before measuring it. Remove leading and trailing whitespace, collapse repeated spaces where your rules allow it, and make sure a title is still meaningful after trimming. A title that looks fine in a spreadsheet can become empty once whitespace is removed.
-
Validate the slug against your rules. Enforce lowercase letters, numbers, and hyphens if that is your standard, and reject anything else before normalization creates surprises. If your workflow derives slugs from titles, confirm the slug is under your chosen maximum length and not duplicated in a way that will create collisions.
-
Run content sanitization checks. Strip or escape tags your WordPress setup should not receive, and reject HTML or shortcodes that your publishing path does not support. If your content pipeline allows rich text, define which tags are allowed instead of trying to clean everything after the API call.
-
Compare the payload to a schema. Use a JSON schema or equivalent rule set so each field has a defined type, length, and constraint. A schema catches drift when someone adds a new field to the automation or changes an upstream source format without telling the publishing step.
-
Block the send if any rule fails. Return a clear error to the automation log with the exact field and reason, such as “slug contains spaces” or “title is empty after trim.” The result should be a rejected payload, not a best-effort request.
That sequence is intentionally boring. Boring validation is good validation. It keeps the failure close to the source and makes the reason obvious before the WordPress REST API is involved.
How should slug validation and title length checks work?
Slug validation should enforce a predictable character set and a length limit, while title checks should guard against emptiness, extremes, and formatting that breaks your editorial standard. The exact numbers are your rule, but they need to be fixed in code, not guessed at runtime.
For slugs, a common pattern is to allow lowercase letters, digits, and hyphens, then reject anything else before normalization creates surprises. If your source title includes punctuation or non-Latin characters, decide whether your automation transliterates them or stops and asks for a manual slug. The trade-off is convenience versus control: automatic slug generation is faster, but manual review gives you cleaner URLs when the input is messy.
For title length, use a lower bound and an upper bound. A one-word title may be too vague for publishing, and an extremely long title can create poor display or downstream truncation issues in themes, feeds, and share cards. WordPress itself will usually store long text, but storage is not the same as good publishing behavior. Set a practical maximum in your own schema and keep it consistent.
One strong pattern is to validate both the raw title and the rendered title. If your automation inserts a prefix like “Article: ” or a category label, measure the final string, not just the source field. A title that passed in the source layer can fail after the automation adds its own text.
Do not rely on WordPress to clean up slug problems for you. Auto-correction creates hidden differences between what the automation thought it sent and what the site actually stored. That makes debugging much harder, especially when the same post data is reused in a queue, a database, or a revision history.
How do content sanitization and schema validation prevent bad posts?
Content sanitization and schema validation prevent bad posts by removing unsafe markup and rejecting payloads that do not match the expected contract. Sanitization cleans the text; schema validation decides whether the structure is acceptable at all. Those are related jobs, but they are not the same job.
Content sanitization matters because post content often comes from multiple sources: an editor, an LLM output, an RSS import, or a CMS export. Each source can produce different markup. If your automation sends HTML, decide whether to allow a limited set of tags such as <p>, <a>, <strong>, and <ul>, or whether to strip everything to plain text. The decision should be explicit, because different WordPress setups will handle markup differently.
Schema validation matters because the API payload itself should have a known shape. A schema can require title to be a string of at least 1 character, slug to match a pattern, and status to be one of a fixed set of values. If the payload contains an unexpected field, the schema can reject it before it causes confusion in your publishing layer.
Use both when you can. Sanitization protects the content body, while schema validation protects the request envelope. If you do only sanitization, you may still send the wrong field names or missing keys. If you do only schema checks, you may still accept content that includes tags your WordPress setup should not receive.
If you want a standard to anchor your validation rules, JSON Schema is the usual reference point for payload structure, and the WordPress REST API documentation explains the post object fields that the endpoint expects. Those two references do not replace your own rules, but they give your automation a stable base to check against.
What common validation mistakes break WordPress automation?
The most common mistakes are validating too late, validating the wrong layer, and treating a successful API request as proof that the post data was correct. Each mistake costs time, because the error appears after the automation has already done work that now has to be undone or retried. For teams building this kind of pipeline, the WordPress REST API handbook and a review of the payload contract can help before the first send.
The first mistake is sending blank-but-present fields. An empty string for title or slug may look harmless in logs, but it is still bad input. The fix is to trim first, then validate emptiness. A field with spaces only should fail immediately.
The second mistake is letting the payload shape drift. If one automation step sends post_title and another sends title, the request may look reasonable to a human and still miss the endpoint’s expected field name. The fix is to define one canonical JSON payload for WordPress and map all upstream fields into that shape before validation runs. This page does not cover the field-mapping step itself; that belongs to a separate workflow design.
The third mistake is skipping length checks on anything that seems “just text.” Titles, slugs, excerpts, and even custom field values can exceed what your downstream tools expect. A 4,000-character content field may be legal in WordPress and still break a preview card generator or a third-party integration. The fix is to validate against the smallest real constraint in the path, not only WordPress’s own storage rules.
The fourth mistake is assuming that sanitization alone solves everything. It does not. Sanitization changes content; it does not confirm business rules. A post can be sanitized and still be unpublishable because the status is invalid or a required taxonomy is missing.
What should a pre-flight validation rule set look like in practice?
A pre-flight rule set should be short, explicit, and tied to the exact JSON payload your automation sends. The goal is to reject bad data in under 1 pass, before the WordPress request is made, and to return an error that points to one field at a time.
A practical rule set often includes these checks:
titleis required, trimmed, and between your chosen minimum and maximum length.slugmatches a safe pattern and does not exceed your limit.contentis present, string-based, and sanitized for your allowed markup.statusis one of the values your workflow allows.- category, tag, author, and custom field IDs exist and are of the correct type.
- unexpected keys are either rejected or ignored by policy, but never silently merged.
Keep the error messages direct. “Invalid payload” is too vague. “slug contains spaces” or “title is empty after trim” is useful because it tells the automation what to fix. If the workflow has multiple sources, include the field source in the log as well, so the failure can be traced back in one step.
This is the point where many teams overbuild the system. They add so many rules that no one can publish a post without engineering help. That is the wrong balance. The right balance is strict on shape and safety, flexible on editorial style. A validator should reject malformed data, not enforce opinions about writing voice.
Key Takeaways
- Validate the payload before the WordPress REST API sees it, not after a request fails.
- Treat
title,content,slug, andstatusas contract fields, not suggestions. - Use both sanitization and schema rules; one cleans content, the other checks structure.
- Enforce slug patterns and title length in your own code so the automation behaves predictably.
- Reject blank, malformed, or unexpected fields early, with an error message that names the exact problem.
What fields should be validated before sending a WordPress post?
Validate the fields that control whether the post can be created cleanly: title, content, slug, status, and any required taxonomy or custom field values. If your automation includes author IDs, featured images, categories, or tags, validate those too. A field is worth checking whenever WordPress or a downstream tool depends on it being present, typed correctly, and within your defined limits.
How do I prevent bad post data from reaching WordPress?
Prevent bad post data by running a pre-flight validation step before the API request is built. Check required fields, trim whitespace, enforce slug and title rules, sanitize content, and compare the payload to a schema. If any rule fails, stop the send and return a clear error tied to the field that caused it.
Drafted with AI; not yet reviewed by a person.