WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I already have a function where a user submits a form and creates a custom post...

<?php $postTitle = $_POST['post_title'];
$submit = $_POST['submit'];

if(isset($submit)){

    global $user_ID;

    $new_post = array(
        'post_title' => $postTitle,
        'post_content' => '',
        'post_status' => 'publish',
        'post_date' => date('Y-m-d H:i:s'),
        'post_author' => '',
        'post_type' => 'stuff',
        'post_category' => array(0)
    );

    $post_id = wp_insert_post($new_post);
add_post_meta($post_id, 'times', '1');

}

I want to check if the custom-post title exists, then if it does NOT, to go ahead and create the post with a #1 in the meta field, and if it does exist, to simply add 1 to the meta field

share|improve this question
up vote 3 down vote accepted

This would need a query.

So building on your code:

<?php
$postTitle = $_POST['post_title'];
$submit = $_POST['submit'];

if(isset($submit)){

    global $user_ID, $wpdb;

    $query = $wpdb->prepare(
        'SELECT ID FROM ' . $wpdb->posts . '
        WHERE post_title = %s
        AND post_type = \'stuff\'',
        $postTitle
    );
    $wpdb->query( $query );

    if ( $wpdb->num_rows ) {
        $post_id = $wpdb->get_var( $query );
        $meta = get_post_meta( $post_id, 'times', TRUE );
        $meta++;
        update_post_meta( $post_id, 'times', $meta );
    } else {
        $new_post = array(
            'post_title' => $postTitle,
            'post_content' => '',
            'post_status' => 'publish',
            'post_date' => date('Y-m-d H:i:s'),
            'post_author' => '',
            'post_type' => 'stuff',
            'post_category' => array(0)
        );

        $post_id = wp_insert_post($new_post);
        add_post_meta($post_id, 'times', '1');
    }
}

Should do it

share|improve this answer
    
You might want to add the AND post_status = 'publish' to the initial query to only return published posts. – Barry Carlyon Jul 15 '12 at 21:09
    
Thanks again for the help! However, the posts aren't making it to the database for some reason.. new or updated-old-ones – marctain Jul 15 '12 at 23:28
    
Is there a typo somewhere? I can't figure it out. – marctain Jul 16 '12 at 3:32
    
I'll have a test when I get home from work – Barry Carlyon Jul 16 '12 at 10:48
    
Epical fail on my part WHERE post_title = %d should read WHERE post_title = %s headdesk – Barry Carlyon Jul 16 '12 at 19:10

WordPress check if post exist by title

function wp_exist_post_by_title( $title ) {
    global $wpdb;
    $return = $wpdb->get_row( "SELECT ID FROM wp_posts WHERE post_title = '" . $title . "' && post_status = 'publish' && post_type = 'post' ", 'ARRAY_N' );
    if( empty( $return ) ) {
        return false;
    } else {
        return true;
    }
}

// usage
if( wp_exist_post_by_title( $post->name ) ) {
// post exist
} else { 
// post does not exist
}
share|improve this answer

You can use the get_page_by_title() function of WordPress:

<?php $postTitle = $_POST['post_title'];
$submit = $_POST['submit'];


if(isset($submit)){
    $customPost = get_page_by_title($postTitle, OBJECT, 'stuff');

    if(!is_null($customPost)) {
       $meta = get_post_meta($customPost->ID, 'times', true);
       $meta++;
       update_post_meta($customPost->ID, 'times', $meta);

       return
    }

    global $user_ID;

    $new_post = array(
        'post_title' => $postTitle,
        'post_content' => '',
        'post_status' => 'publish',
        'post_date' => date('Y-m-d H:i:s'),
        'post_author' => '',
        'post_type' => 'stuff',
        'post_category' => array(0)
    );

    $post_id = wp_insert_post($new_post);
    add_post_meta($post_id, 'times', '1');

}
share|improve this answer
    
Hey Alex, welcome to WPSE. Please feel free to take the tour. We like to live by the principle here of teaching people to fish rather than just handing the fish to them. Would you mind editing your post to add some explanation of why this solves the OP's problem? – Tim Malone May 16 at 10:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.