Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




post_exists [ WordPress Function ]

post_exists ( $title, $content = '', $date = '' )
Parameters:
  • (string) $title Post title
  • (string) $content Optional post content
  • (string) $date Optional post date
Returns:
  • (int) Post ID if post exists, 0 otherwise.
Defined at:



Determine if a post exists based on title, content, and date

Source


<?php
function post_exists($title$content ''$date '') {
    global 
$wpdb;

    
$post_title stripslashessanitize_post_field'post_title'$title0'db' ) );
    
$post_content stripslashessanitize_post_field'post_content'$content0'db' ) );
    
$post_date stripslashessanitize_post_field'post_date'$date0'db' ) );

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

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

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

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

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

    return 
0;
}
?>

Examples [ wp-snippets.com ]

Top Google zoekresultaten

Meer ...

Gebruikersdiscussies [ wordpress.org ]

0 User Note(s)

Nog geen één. Wees de eerste!

Nieuw toevoegen ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics