Make WordPress Core

Ticket #35246: post_exists.php

File post_exists.php, 866 bytes (added by yetAnotherDaniel, 10 years ago)

new post_exists function

Line 
1<?php
2
3function post_exists($title, $content = '', $date = '') {
4        global $wpdb;
5
6        $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
7        $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
8        $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
9
10        $query = "SELECT ID FROM $wpdb->posts WHERE ";
11        $where = array();
12        $args = array();
13
14        if ( !empty ( $date ) ) {
15                $where[] = 'post_date = %s';
16                $args[] = $post_date;
17        }
18
19        if ( !empty ( $title ) ) {
20                $where[] = 'post_title = %s';
21                $args[] = $post_title;
22        }
23
24        if ( !empty ( $content ) ) {
25                $where[] = 'post_content = %s';
26                $args[] = $post_content;
27        }
28
29        $query .= implode( ' AND ', $where );
30
31        if ( !empty ( $args ) )
32                return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
33
34        return 0;
35}