Skip to content
🛠 How to check if a post exists in WordPress: get_post, get_post_status and post_exists

🛠 How to check if a post exists in WordPress: get_post, get_post_status and post_exists

We updated the plugin, and the site went down. A user writes: "My post disappeared." But it's visible in the admin panel. Sound familiar?

The first instinct is to go into the database and look with your own eyes. But when there are thousands of posts and you need a programmatic check (API import, duplicate protection, conditional logic in a plugin), you need standard WordPress functions.

There are three: get_post(), get_post_status(), and post_exists(). Each one covers its own scenario, and picking the wrong one means extra database load or a false positive. In the next 7 minutes, you'll learn which one to use and get ready-made snippets for functions.php and plugins.

💡 Quick overview:

  • Checking by ID with get_post and get_post_status
  • Checking by title with post_exists
  • Three typical scenarios with ready-to-use code for functions.php

How WordPress stores posts and why you need to check their existence at all

WordPress keeps all posts in the wp_posts table. Each one has a unique numeric ID, a title string, content, a type (post, page, product, and any custom type), and a status: publish, draft, trash, auto-draft, and about a dozen more.

You need to check a post's existence programmatically in three cases. First: you're writing code that updates or deletes a post by ID; if the post was already deleted manually, wp_update_post() with a nonexistent ID will silently create a new one, and wp_delete_post() will return false without a warning. Second: importing content from a CSV or an external API; without a title check, you'll generate duplicates. Third: batch processing hundreds of IDs in a loop; here, choosing the lightest function is critical so you don't crush the database with extra queries.

Now let's break down each function with code you can copy and paste.

Checking by ID: get_post(), simple but heavy on the database

get_post(), the basic WordPress function for retrieving a post by ID. It accepts an ID or an existing WP_Post object and returns either a full post object with all fields or null.

1$post_id = 42;
2$post = get_post( $post_id );
3
4if ( $post && 'trash' !== $post->post_status ) {
5 // Record exists and is not in trash
6 echo 'Found: ' . esc_html( $post->post_title );
7} else {
8 // No record, or it is deleted
9 error_log( 'Post #' . $post_id . ' not found.' );
10}

// What's happening here: we get the post by ID, check that it's not null and that the status is not trash; otherwise, a deleted post would pass the check as "existing."

Why get_post() is not the best choice for a simple check. The function runs a SELECT * on the wp_posts table, pulls all fields from post_title to post_modified_gmt and to_ping, then builds a WP_Post object and passes it through the the_post filter. When all you need is "yes or no," loading the full object is a waste of memory and CPU time. On a single call, the difference is unnoticeable. In a loop of 500 iterations, it's noticeable.

Nevertheless, get_post() is justified if, right after the check, you work with the post's title, content, or meta fields. One call covers both tasks: the existence check and getting the data.

Checking by ID: get_post_status(), lighter and noticeably faster

When you only need to know if a post is alive, use get_post_status(). It makes a single narrow query SELECT post_status FROM wp_posts WHERE ID = ... and returns a status string or false.

1$post_id = 42;
2
3if ( get_post_status( $post_id ) ) {
4 // Record exists — regardless of publish, draft, or trash
5 echo 'Status: ' . get_post_status( $post_id );
6}

// Unlike get_post(), no WP_Post object is created here and no theme filters are called, just a bare status query. That's more than enough for an existence check.

Scenario: safe product synchronization. You pull prices from an external API once an hour and update the corresponding WooCommerce products. Between synchronizations, a product could have been deleted manually. Before wp_update_post(), we check that the ID is alive:

1$remote_product_id = 152;
2$post_id = get_post_id_by_meta( 'remote_id', $remote_product_id );
3
4if ( $post_id && get_post_status( $post_id ) ) {
5 wp_update_post( array(
6 'ID' => $post_id,
7 'post_title' => 'Product updated: ' . current_time( 'mysql' ),
8 ) );
9} else {
10 // Old record deleted — creating a new one
11 wp_insert_post( array(
12 'post_title' => 'New product',
13 'post_status' => 'publish',
14 ) );
15}

// Note: get_post_status() returns false only if the post physically doesn't exist. The trash status is a valid status, and the function will return the string 'trash'. If you need to exclude the trash, add an explicit check 'trash' !== get_post_status( $post_id ).

On a dry run of 1,000 checks, get_post_status() completes noticeably faster than get_post(); allocating the WP_Post object and calling the_post consumes almost half the time.

Checking by title: post_exists(), when you have no ID and don't want a duplicate

post_exists() searches for a post by title, content, date, type, and status. It returns the ID of the found post or 0.

1post_exists( $title, $content = '', $date = '', $type = '', $status = '' );

// The only required parameter is $title. The rest are optional and narrow the search. If there are multiple matches, the function returns the ID of the first post found.

Basic example:

1$post_title = 'How to choose WordPress hosting';
2$existing_id = post_exists( $post_title );
3
4if ( $existing_id ) {
5 echo 'Post exists, ID: ' . $existing_id;
6 // Not creating a duplicate — updating the existing one
7}

Scenario: duplicate protection during a CSV import. You're loading 200 articles from a file. Some titles may match already published ones. post_exists() before each wp_insert_post() solves the problem:

1$items = array(
2 array( 'title' => 'Plugin A review', 'content' => '...' ),
3 array( 'title' => 'Plugin B review', 'content' => '...' ),
4);
5
6foreach ( $items as $item ) {
7 $exists = post_exists( $item['title'], $item['content'] );
8
9 if ( ! $exists ) {
10 wp_insert_post( array(
11 'post_title' => $item['title'],
12 'post_content' => $item['content'],
13 'post_status' => 'publish',
14 ) );
15 }
16}

// Important: post_exists() makes a direct SQL query via $wpdb->get_var() and does not account for custom statuses registered by third-party plugins. If you're working with non-standard post types, check existence via WP_Query.

Also, post_exists() is case-sensitive: "Overview" and "overview" are different strings for MySQL depending on the table collation. Before calling, normalize titles to a single case using mb_strtolower().

Scenario: batch checking hundreds of IDs. When the input is an array of IDs and you need to filter out nonexistent ones, we build a single SQL query instead of a hundred get_post_status() calls:

1$ids_to_check = array( 1, 2, 42, 99, 156 );
2$placeholders = implode( ',', array_fill( 0, count( $ids_to_check ), '%d' ) );
3
4$existing_ids = $wpdb->get_col(
5 $wpdb->prepare(
6 "SELECT ID FROM {$wpdb->posts} WHERE ID IN ( $placeholders )",
7 $ids_to_check
8 )
9);
10
11// $existing_ids — only IDs that actually exist in the database
12$missing_ids = array_diff( $ids_to_check, $existing_ids );

// One query instead of N; on an array of 500 IDs, this saves seconds of execution time. The downside: direct SQL bypasses the WordPress object cache, so for single checks we stick with standard functions.

If video is more convenient, this lesson covers how get_post() works inside a WordPress theme: how to get a post outside the main loop, output its content, and not break the global $post object.

⁉️🤔 Frequent questions

Can you check a post's existence without any WordPress functions at all?

You can: $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE ID = $post_id" ), the lightest method, just ID or NULL. But direct SQL bypasses the object cache and other plugins' filters. Overkill for a single check; justified for batch processing (see the scenario above).

Which is faster: get_post_status() or get_post()?

get_post_status() is noticeably faster. It does a SELECT post_status on a single column, while get_post() does a SELECT * followed by building a WP_Post object and calling the the_post filter. The difference adds up in loops and on high-traffic sites.

Does post_exists() search across all post types?

By default, only among post. Pass $type explicitly: 'page', 'product', or an empty string '' to search across all types. For custom types with non-standard statuses, WP_Query is more reliable.

Does get_post() return a post from the trash?

Yes. The trash status is just as valid a status as publish. get_post( $trashed_id ) will return a full-fledged object. Exclude the trash with an explicit check 'trash' !== $post->post_status or use get_post_status().

Is it safe to use post_exists() in real time before wp_insert_post()?

Almost always, yes. But with simultaneous requests from two processes, a race condition is possible: both will see "no post exists" and create a duplicate. For critical operations, add a unique index in the database or a transaction. In a typical import, the probability of a race condition is near zero.

How do you check a post's existence by a custom field, not by title?

There is no standard function. Use WP_Query with meta_query or a direct $wpdb query to wp_postmeta. For frequent checks on the same field, add an index on meta_key + meta_value; otherwise, the query will scan the entire meta table.

Which function fits your task

The choice comes down to three questions. Checking by ID and don't need the post data? get_post_status(), minimal query, maximum speed. Right after the check, do you work with the title or content? get_post() will both check and return the data in one call. No ID, but you have a title? post_exists() with duplicate protection.

For a typical workflow of "update a post if it exists; create if it doesn't," use the combination of get_post_status() + wp_update_post() or wp_insert_post() with branching. Don't overload your code with defensive checks where they're redundant: a single wp_insert_post() with a unique title is often sufficient on its own.

And most importantly: if you're writing code for a public plugin or theme, always check the post's existence. The user can delete it between your calls, and an unchecked wp_update_post() will silently create a junk post instead of updating the existing one.