rainerbrunotte
rainerbrunotte

Reputation: 907

WP check if custom post title exists before wp_insert_post

I am trying to do a conditional check (checking if post with same post title exists) before running wp_insert_post() but it still accepts the insertion even if existing posts are found. I tried using post_exists(), but this function breaks the entire page for some reason. What am I doing wrong?

footer.php:

<?php

$statusmessage = '';

if (isset($_POST['footer-email'])){

$footer_email = $_POST['footer-email'];

$args = array(
"post_title" => $footer_email,
"post_type" => 'subscriptions',
"post_status" => 'publish',
);
$query = new WP_Query( $args );

if($query->have_posts()) {

$statusmessage = '<p class="subscription-confirmation">You have already signed up.</p>';

} 
    
else {
      
$my_post = array(
'post_title' => "$footer_email",
'post_content' => "$footer_email",
'post_type' => 'subscribers',
'post_status' => 'publish',
);

$post_id =  wp_insert_post( $my_post );

$statusmessage = '<p class="subscription-confirmation">You have successfully signed up.</p>';
            
} // end else

wp_reset_postdata();

} // end if isset footer email

?>

Upvotes: 0

Views: 861

Answers (1)

Bhautik
Bhautik

Reputation: 11282

You can use post_exists(). check the below cood.

<?php

    $statusmessage = '';

    if (isset($_POST['footer-email'])){

        $footer_email = $_POST['footer-email'];

        if( ! post_exists( $footer_email ) ){

            $my_post = array(
                'post_title'   => "$footer_email",
                'post_content' => "$footer_email",
                'post_type'    => 'subscribers',
                'post_status'  => 'publish',
            );

            $post_id =  wp_insert_post( $my_post );

            $statusmessage = '<p class="subscription-confirmation">You have successfully signed up.</p>';

        }
                    
    } // end if isset footer email

You can use get_page_by_title() to fetch subscribers by title.

$statusmessage = '';

if ( isset( $_POST['footer-email'] ) ){

    $footer_email = $_POST['footer-email'];
    
    $subscribers = get_page_by_title( $footer_email , OBJECT, 'subscribers' );

    if( !$subscribers->ID ){

        $my_post = array(
            'post_title'   => "$footer_email",
            'post_content' => "$footer_email",
            'post_type'    => 'subscribers',
            'post_status'  => 'publish',
        );

        $post_id =  wp_insert_post( $my_post );

        $statusmessage = '<p class="subscription-confirmation">You have successfully signed up.</p>';

    }
}           

OR custom wpdb query.

global $wpdb;

$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $footer_email . "'" );

if( !$postid ){

    $my_post = array(
        'post_title'   => "$footer_email",
        'post_content' => "$footer_email",
        'post_type'    => 'subscribers',
        'post_status'  => 'publish',
    );

    $post_id =  wp_insert_post( $my_post );

    $statusmessage = '<p class="subscription-confirmation">You have successfully signed up.</p>';

}

Upvotes: 1

Related Questions