Check if post_content already exist in database

290 Views Asked by At

I need a function to check if post_content already exist in database.

Wordpress built in function post_exists() checks by post post_title.

I need to check by post_content regardless of the post_title.

There such a function exist?

How can I solve this?

Thank you for your help

1

There are 1 best solutions below

0
On BEST ANSWER

It looks like a small variation on post_exists() should work. Create a function like this in your child theme's functions.php, and then use it instead of post_exists():

function post_exists_by_content($content) {
  global $wpdb;

  $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );

  $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
  $args = array();

  if ( !empty ( $content ) ) {
    $query .= ' AND post_content = %s';
    $args[] = $post_content;
  }

  if ( !empty ( $args ) )
    return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );

  return 0;
}