Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

up vote 2 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

before increase post meta, at first you need to find if POST TITLE already exists

method 1)

$title = 'mytitlee';

global $wpdb;
$id_ofpost_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$title'");
$id_ofpost_title = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title'");
if ($id_ofpost_name || $id_ofpost_title) {echo 'Exists, here is the id:'.$id_ofpost_title.' or '.$id_ofpost_name;} 
else {echo 'post wasnt found';}

method 2) (this method is without Mysql query, so it may be a bit slower).

p.s. in the end, uncomment post meta increasement if you want:

$title = 'my_slug';
//you can use post_types too
$ptype='';

global $wpdb;
global $post;
$args = array( 'post_type' => $ptype);
$allPosts = get_posts($args);
$smthh='no';
foreach( $allPosts as $page )
{
    if( $smthh == 'no' && ($page->post_name == $title ||  $page->post_title == $title) )
    {$smthh='yes'; break; }
}


if ( $smthh == 'yes' )
{ 
    echo "yes, post was found";
}

else
{
    echo "no, not found";

    // You can insert the post too
    $my_post = array( 'post_type'    => $ptype, 'post_title'    => $title, 'post_content'  => 'This is my post content.', 'post_status'   => 'publish', );
    wp_insert_post( $my_post );
    //add_post_meta($post_id, 'times', '1');
}
share|improve this answer
1  
You are going to loop through all posts to find out if a single slug exists? And you continue the loop even after the slug has been found. That is very labor intensive. A single SQL query can find this with no loop at all. Also, when you answer please post an explanation along with the code, and not just a raw code dump. –  s_ha_dum Apr 8 '13 at 13:08

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.